zoukankan      html  css  js  c++  java
  • 为项目绑定附件

    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;
                }
            }
  • 相关阅读:
    面向对象程序设计寒假作业2
    面向对象程序设计寒假作业1
    自我介绍
    3组-Alpha冲刺-1/6
    3组 需求分析报告
    3组 团队展示
    第一次个人编程作业
    第一次博客作业
    我罗斯方块最终篇
    我罗斯方块设计
  • 原文地址:https://www.cnblogs.com/yixiaozi/p/3844152.html
Copyright © 2011-2022 走看看