[C#] 上传文件到SharePoint文档库 →→→→→进入此内容的聊天室

来自 , 2019-08-08, 写在 C#, 查看 126 次.
URL http://www.code666.cn/view/f8da71e5
  1. /*
  2. SharePoint的文档库是根据数据库虚拟出来的,以HTTP形式呈现,因要创建一个页面单独实现上传功能,故对于其存储和呈现机制进行了学习和研究,不过网络上相关资料还真是很少。SharePoint个人觉得还是比较适合不需要进行复杂逻辑功能的二次开发的网站构建,即适合一般基于office组件的功能门户,能够极大提高效率。
  3. http://download.csdn.net/detail/keai1365/207303
  4. 以下代码能够实现往文档库下层目录上传文件的功能,主要还是参考网络上其他文章
  5. */
  6.  
  7. using System.IO;
  8. using Microsoft.SharePoint;
  9. using System.Web;
  10.  
  11. namespace ConsoleApplication1
  12. {
  13.     class Program
  14.     {
  15.         static void Main(string[] args)
  16.         {
  17.             string fileName = @"C:\Documents and Settings\Administrator\桌面\word_test.docx";
  18.             FileInfo myfile = new FileInfo(fileName);              
  19.             byte[] fileContents = new byte[int.Parse(myfile.Length.ToString())];
  20.             FileStream fs=File.OpenRead(fileName);
  21.             int n = fs.Read(fileContents, 0, int.Parse(myfile.Length.ToString()));
  22.             string result = Program.UploadDocument("word_test.docx", fileContents, @"http://zpkxv7t0p3xxqyg:47024/DocLib1/New_Test");          
  23.         }
  24.  
  25.         public static string UploadDocument(string fileName, byte[] fileContents, string pathFolder)
  26.         {
  27.             if (fileContents == null)
  28.                 return "Null Attachment";
  29.             try
  30.             {
  31.                 int iStartIndex = pathFolder.LastIndexOf("/");              
  32.                 string sitePath = pathFolder.Remove(iStartIndex);
  33.                 string folderName = string.Empty;
  34.  
  35.                 if (sitePath.LastIndexOf("/") > 6)
  36.                 {
  37.                     int sec_iStartIndex = sitePath.LastIndexOf("/");
  38.                     // System.Console.WriteLine(sec_iStartIndex);
  39.                     sitePath = pathFolder.Remove(sitePath.LastIndexOf("/"));
  40.                     folderName = pathFolder.Substring(sec_iStartIndex + 1);
  41.                   //  System.Console.WriteLine(folderName);
  42.                     // System.Console.WriteLine(folderName);
  43.                     // System.Console.WriteLine(sitePath);
  44.                 }
  45.                 //   string folderName = pathFolder.Substring(iStartIndex + 1);
  46.                 //   System.Console.WriteLine(folderName);
  47.                 else
  48.                 {
  49.                     folderName = pathFolder.Substring(iStartIndex + 1);
  50.                    // System.Console.WriteLine(folderName);
  51.                 }
  52.                 SPSite site = new SPSite(sitePath);
  53.                 SPWeb web = site.OpenWeb();
  54.                  
  55.                
  56.                 SPFolder folder = web.GetFolder(folderName);
  57.  
  58.                 string fileURL = fileName;
  59.                  
  60.                 folder.Files.Add(fileURL, fileContents);
  61.  
  62.                 if (folder.Files[fileURL].CheckedOutBy.Name != "")
  63.                 {
  64.                     folder.Files[fileURL].CheckIn("File Checked In");
  65.                 }
  66.                 return "File added successfully!";
  67.             }
  68.             catch (System.Exception ex)
  69.             {
  70.                 return ex.Source + ":" + ex.Message;
  71.             }
  72.         }
  73.     }
  74. }

回复 "上传文件到SharePoint文档库"

这儿你可以回复上面这条便签

captcha