sahrepoint 上传到文档库

sharepoint学习笔记汇总

http://blog.csdn.net/qq873113580/article/details/20390149

 

 

 

 

/// <summary>
        /// 上传附件到文档库(包括删除获取)
        /// </summary>
        /// <param name="dirName"></param>
        /// <param name="fileUpload"></param>
        /// <returns></returns>
        private string GetFileUrl(string dirName, FileUpload fileUpload)
        {
            string url = "";
            if (fileUpload.HasFile == false)
            {
                return url;
            }
            using (SPWeb rootWeb = new SPSite(Common.rootUrl).OpenWeb())
            {
                //找到文档库的位置
                SPList libList = rootWeb.GetList(Common.rootUrl + "/ReportFile/Forms/AllItems.aspx");
                //找到文档库根目录
                SPFolder rootFolder = libList.RootFolder;

                //子目录
                SPFolder subFolder = null;

                #region 不存在文件夹就新建
                bool exists = false;
                //根目录下的文件夹集合
                SPFolderCollection folders = rootFolder.SubFolders;
                foreach (SPFolder folde in folders)
                {
                    if (folde.Name == dirName)
                    {
                        exists = true;
                        break;
                    }
                }
                if (exists == false)
                {
                    //不存在就在根目录下添加一个文件夹
                    subFolder = rootFolder.SubFolders.Add(rootFolder.Url + "/" + dirName);
                }
                else
                {
                    //存在就直接读取这个文件夹
                    subFolder = rootFolder.SubFolders[dirName];
                }
                #endregion

 

                #region 删除子目录下的所有文件在添加
                SPFileCollection files = subFolder.Files;
               for (int i = files.Count - 1; i >= 0; i--)
                {
                    files[i].Delete();
                }
                #endregion
                //获取文件流
                Stream fStream = fileUpload.PostedFile.InputStream;
                byte[] contents = new byte[fStream.Length];
                fStream.Read(contents, 0, (int)fStream.Length);
                //保存到文档库
                subFolder.Files.Add(fileUpload.FileName, contents);
                fStream.Close();
                //获取上传之后的文件位置http://xxx/xx/xx.xx
                url = Common.rootUrl + "/" + subFolder.Url + "/" + fileUpload.FileName;
            }
            return url;
        }

 

发布了230 篇原创文章 · 获赞 184 · 访问量 35万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览