用WebClinet实现SharePoint上文档库中文件的上传与下载

来源:互联网 发布:中国网络信息安全 编辑:程序博客网 时间:2024/05/21 06:17
微软的SharePoint 提供了强大的文档管理功能,能够创建各种类型的文档库,并对文档进行相应的管理。所以我们的产品也打算将文件用SharePoint来管理,实现文档的共享访问。于是,就产生了用客户端程序访问SharePoint上的文档库来上传下载文件的需求。我就用C#中的WebClient类写了一个实现SharePoint上文件的上传与下载的类。下面是该类的代码,里面有详细的注释。而且,使用起来非常简单,只要传入相应的参数就可以实现向SharePoint上传文件,和从SharePoint下载文件。由于是用WebClient实现的,所以也适用于普通网站的文件上传和下载,当然前提是要有相应的权限。
  1. using System;  
  2. using System.IO;  
  3. using System.Net;  
  4. using System.Collections.Specialized;  
  5.   
  6. namespace SharePointVisitUtilities  
  7. {  
  8.     public static class SharePointFileHelper  
  9.     {  
  10.         // 上传文件  
  11.         //   
  12.         // 参数  
  13.         // 上传的文件在SharePoint上的位置,要上传的本地文件的路径名,用户名,密码,域  
  14.         public static string UploadFile(string strDestUrl, string strFilePathName, string strUserName, string strPassword, string strDomain)  
  15.         {  
  16.             string strResult = "Success";  
  17.   
  18.             try  
  19.             {  
  20.                 string strFileName = Path.GetFileName(strFilePathName);  
  21.                 string strCopiedFilePathName = Path.GetTempPath() + strFileName;  
  22.   
  23.                 // 将文件拷贝到临时文件夹  
  24.                 // 目的是可以在文件在被打开状态下还可以上传  
  25.                 File.Copy(strFilePathName, strCopiedFilePathName, true);  
  26.   
  27.                 // 打开拷贝到临时目录下的文件  
  28.                 FileStream fs = new FileStream(strCopiedFilePathName, FileMode.Open, FileAccess.Read);  
  29.   
  30.                 // 读文件  
  31.                 BinaryReader br = new BinaryReader(fs);  
  32.                 Byte[] filecontents = br.ReadBytes((int)fs.Length);  
  33.   
  34.                 br.Close();  
  35.                 fs.Close();  
  36.   
  37.                 WebClient webclient = CreateWebClient(strUserName, strPassword, strDomain);  
  38.                 
  39.                 // 上传  
  40.                 webclient.UploadData(strDestUrl + strFileName, "PUT", filecontents);  
  41.             }  
  42.             catch (Exception ex)  
  43.             {  
  44.                 strResult = "Failed! " + ex.Message;  
  45.             }  
  46.   
  47.             return strResult;  
  48.         }  
  49.   
  50.         // 下载文件  
  51.         //   
  52.         // 参数  
  53.         // 下载的文件在SharePoint上的位置,文件下载后存放的本地文件夹路径,用户名,密码,域  
  54.         public static string DownloadFile(string strSourceUrl, string strDestFolder, string strUserName, string strPassword, string strDomain)  
  55.         {  
  56.             string strResult = "Success";  
  57.   
  58.             try  
  59.             {  
  60.                 WebClient webclient = CreateWebClient(strUserName, strPassword, strDomain);  
  61.   
  62.                 // 下载  
  63.                 Byte[] filecontents = webclient.DownloadData(strSourceUrl);  
  64.   
  65.                 string strFileName = Path.GetFileName(strSourceUrl);  
  66.   
  67.                 // 创建文件  
  68.                 FileStream fs = new FileStream(strDestFolder + strFileName, FileMode.Create, FileAccess.Write);  
  69.                 // 写文件  
  70.                 fs.Write(filecontents, 0, filecontents.Length);  
  71.                 fs.Close();  
  72.             }  
  73.             catch (Exception ex)  
  74.             {  
  75.                 strResult = "failed! " + ex.Message;  
  76.             }  
  77.   
  78.             return strResult;  
  79.         }  
  80.   
  81.         // 创建WebClient  
  82.         // 参数:用户名,密码,域(用来登陆SharePoint)  
  83.         private static WebClient CreateWebClient(string strUserName, string strPassword, string strDomain)  
  84.         {  
  85.             WebClient webclient = new WebClient();  
  86.   
  87.             if (String.IsNullOrEmpty(strUserName))  
  88.             {  
  89.                 webclient.UseDefaultCredentials = true;  
  90.             }  
  91.             else  
  92.             {  
  93.                 NetworkCredential credential = new NetworkCredential(strUserName, strPassword, strDomain);  
  94.                 webclient.Credentials = credential;  
  95.             }  
  96.   
  97.             return webclient;  
  98.         }  
  99.     }  


原创粉丝点击