微软的SharePoint 提供了强大的文档管理功能,能够创建各种类型的文档库,并对文档进行相应的管理。所以我们的产品也打算将文件用SharePoint来管理,实现文档的共享访问。于是,就产生了用客户端程序访问SharePoint上的文档库来上传下载文件的需求。我就用C#中的WebClient类写了一个实现SharePoint上文件的上传与下载的类。下面是该类的代码,里面有详细的注释。而且,使用起来非常简单,只要传入相应的参数就可以实现向SharePoint上传文件,和从SharePoint下载文件。由于是用WebClient实现的,所以也适用于普通网站的文件上传和下载,当然前提是要有相应的权限。
- using System;
- using System.IO;
- using System.Net;
- using System.Collections.Specialized;
- namespace SharePointVisitUtilities
- {
- public static class SharePointFileHelper
- {
- // 上传文件
- //
- // 参数
- // 上传的文件在SharePoint上的位置,要上传的本地文件的路径名,用户名,密码,域
- public static string UploadFile(string strDestUrl, string strFilePathName, string strUserName, string strPassword, string strDomain)
- {
- string strResult = "Success";
- try
- {
- string strFileName = Path.GetFileName(strFilePathName);
- string strCopiedFilePathName = Path.GetTempPath() + strFileName;
- // 将文件拷贝到临时文件夹
- // 目的是可以在文件在被打开状态下还可以上传
- File.Copy(strFilePathName, strCopiedFilePathName, true);
- // 打开拷贝到临时目录下的文件
- FileStream fs = new FileStream(strCopiedFilePathName, FileMode.Open, FileAccess.Read);
- // 读文件
- BinaryReader br = new BinaryReader(fs);
- Byte[] filecontents = br.ReadBytes((int)fs.Length);
- br.Close();
- fs.Close();
- WebClient webclient = CreateWebClient(strUserName, strPassword, strDomain);
- // 上传
- webclient.UploadData(strDestUrl + strFileName, "PUT", filecontents);
- }
- catch (Exception ex)
- {
- strResult = "Failed! " + ex.Message;
- }
- return strResult;
- }
- // 下载文件
- //
- // 参数
- // 下载的文件在SharePoint上的位置,文件下载后存放的本地文件夹路径,用户名,密码,域
- public static string DownloadFile(string strSourceUrl, string strDestFolder, string strUserName, string strPassword, string strDomain)
- {
- string strResult = "Success";
- try
- {
- WebClient webclient = CreateWebClient(strUserName, strPassword, strDomain);
- // 下载
- Byte[] filecontents = webclient.DownloadData(strSourceUrl);
- string strFileName = Path.GetFileName(strSourceUrl);
- // 创建文件
- FileStream fs = new FileStream(strDestFolder + strFileName, FileMode.Create, FileAccess.Write);
- // 写文件
- fs.Write(filecontents, 0, filecontents.Length);
- fs.Close();
- }
- catch (Exception ex)
- {
- strResult = "failed! " + ex.Message;
- }
- return strResult;
- }
- // 创建WebClient
- // 参数:用户名,密码,域(用来登陆SharePoint)
- private static WebClient CreateWebClient(string strUserName, string strPassword, string strDomain)
- {
- WebClient webclient = new WebClient();
- if (String.IsNullOrEmpty(strUserName))
- {
- webclient.UseDefaultCredentials = true;
- }
- else
- {
- NetworkCredential credential = new NetworkCredential(strUserName, strPassword, strDomain);
- webclient.Credentials = credential;
- }
- return webclient;
- }
- }
- }