sahrepoint 上傳到文檔庫
sharepoint學習筆記匯總
http://blog.csdn.net/qq873113580/article/details/20390149
?
?
?
?
/// <summary>
??????? /// 上傳附件到文檔庫(包含刪除獲取)
??????? /// </summary>
??????? /// <param name="dirName"></param>
??????? /// <param name="fileUpload"></param>
??????? /// <returns></returns>
??????? {
??????????? 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 不存在文件夾就新建
??????????????? //根文件夾下的文件夾集合
??????????????? 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;
??????? }
?
sahrepoint 上傳到文檔庫