• 为项目绑定附件


    protected void OkButton_Click(object sender, EventArgs e)
            {
                if (fileUpload.HasFile)
                {
                    string strLoginName = HttpContext.Current.User.Identity.Name.ToString();    //获取用户名
                    string folderTemp = strLoginName.Substring(strLoginName.LastIndexOf('\') + 1);
                    SPSecurity.RunWithElevatedPrivileges(delegate()
                        {
                            try
                            {
                                string FileFolder = Server.MapPath("~/_layouts/images/" + folderTemp + "Upfile/");
                                if (!Directory.Exists(FileFolder))     //根目录
                                {
                                    Directory.CreateDirectory(FileFolder);//判断上传目录是否存在     自动创建
                                }

                                string strFilePath = FileFolder + fileUpload.FileName;
                                fileUpload.SaveAs(Server.MapPath("~/_layouts/images/" + folderTemp + "Upfile/" + fileUpload.FileName));
                                AddAttachment(strFilePath);

                            }
                            catch (Exception ex)
                            {
                                throw ex;
                            }
                            finally
                            {
                                string strFileFolder = Server.MapPath("~/_layouts/images/" + folderTemp + "Upfile/");
                                if (Directory.Exists(strFileFolder))     //根目录
                                {
                                    //Directory.CreateDirectory(strFileFolder);//判断上传目录是否存在     自动创建
                                    Directory.Delete(strFileFolder, true);
                                }

                            }
                        });
                }
                else
                {
                    labMess.Visible = true;
                }
            }






    public void AddAttachment(string originalImagePath)
            {
                int taskID = int.Parse(hfTaskID.Value);
                string strUrl = hfProjectUrl.Value;
                try
                {
                    using (SPWeb spWeb = new SPSite(strUrl).OpenWeb())
                    {
                        SPList spList = spWeb.Lists["任务"];
                        SPQuery oQuery = new SPQuery();
                        oQuery.Query = "<Where><Eq><FieldRef Name='ID' /><Value Type='Counter'>" + taskID + "</Value></Eq></Where>";
                        SPListItemCollection listItemCollection = spList.GetItems(oQuery);
                        if (listItemCollection.Count > 0)
                        {
                            SPListItem item = listItemCollection[0];
                            item["Status"] = "已提交";
                            string fileName = fileUpload.FileName;
                            SPAttachmentCollection attach = item.Attachments;
                            if (attach != null)
                            {
                                for (int i = 0; i < attach.Count; i++)
                                {
                                    if (attach[i] == fileName)
                                    {
                                        attach.Delete(attach[i]);
                                    }
                                }
                            }
                            hfProjectTitle.Value = fileName;
                            //HttpPostedFile UpFile = fileUpload.PostedFile;
                            item.Attachments.Add(fileName, GetAttachmentData(originalImagePath));
                            spWeb.AllowUnsafeUpdates = true;
                            item.Update();
                        }
                    }
                    SendInForm();
                }
                catch (Exception e)
                {
                    throw e;
                }
            }

    public byte[] GetAttachmentData(string originalImagePath)
            {
                try
                {
                    using (FileStream fStream = System.IO.File.OpenRead(originalImagePath))
                    {
                        //Stream StreamObject = UpFile.InputStream;//建立数据流对像
                        byte[] btData = new byte[fStream.Length];
                        fStream.Read(btData, 0, btData.Length);
                        fStream.Close();
                        return btData;
                    }
                }
                catch (Exception e)
                {
                    return null;
                }
            }
  • 相关阅读:
    博士考试复习过程总结
    制作在Linux、Unix上以daemon方式启动Apusic的详细步骤(转)
    重开Blog
    给计算机的兄弟姐妹们补补身体→煲银耳粥
    被人夸成像设计师,高兴呐
    今天问题总结(Hibernate配置参数访问Oracle,Linux下的Apusic自启动)
    HDU ACM 1272 小希的迷宫
    Uva 10115 Automatic Editing
    HDU ACM 1162 Eddy's picture(MST)
    Uva 10474 Where is the Marble?(水题)
  • 原文地址:https://www.cnblogs.com/yixiaozi/p/3844152.html
Copyright © 2020-2023  润新知