用WebClinet实现SharePoint上文档库中文件的上传与下载
用WebClinet实现SharePoint上文档库中文件的上传与下载
微软的SharePoint 提供了强大的文档管理功能,能够创建各种类型的文档库,并对文档进行相应的管理。所以我们的产品也打算将文件用SharePoint来管理,实现文档的共享访问。于是,就产生了用客户端程序访问SharePoint上的文档库来上传下载文件的需求。我就用C
用WebClinet实现SharePoint上文档库中文件的上传与下载
微软的SharePoint 提供了强大的文档管理功能,能够创建各种类型的文档库,并对文档进行相应的管理。所以我们的产品也打算将文件用SharePoint来管理,实现文档的共享访问。于是,就产生了用客户端程序访问SharePoint上的文档库来上传下载文件的需求。我就用C#中的WebClient类写了一个实现SharePoint上文件的上传与下载的类。下面是该类的代码,里面有详细的注释。而且,使用起来非常简单,只要传入相应的参数就可以实现向SharePoint上传文件,和从SharePoint下载文件。由于是用WebClient实现的,所以也适用于普通网站的文件上传和下载,当然前提是要有相应的权限。
using System;
using System.IO;
using ;
using System.Collecti.Specialized;namespace SharePointVisitUtilities
{public static class SharePointFileHelper{// 上传文件// // 参数// 上传的文件在SharePoint上的位置,要上传的本地文件的路径名,用户名,密码,域public static string UploadFile(string strDestUrl, string strFilePathame, string strUserame, string strPassword, string strDomain){string strResult = Success;try{string strFileame = Path.GetFileame(strFilePathame);string strCopiedFilePathame = Path.GetTempPath() strFileame;// 将文件拷贝到临时文件夹// 目的是可以在文件在被打开状态下还可以上传File.Copy(strFilePathame, strCopiedFilePathame, true);// 打开拷贝到临时目录下的文件FileStream fs = new FileStream(strCopiedFilePathame, FileMode.Open, FileAccess.Read);// 读文件BinaryReader br = new BinaryReader(fs);Byte[] filecontents = br.ReadBytes((int)fs.Length);br.Close();fs.Close();WebClient webclient = CreateWebClient(strUserame, strPassword, strDomain);// 上传webclient.UploadData(strDestUrl strFileame, PUT, filecontents);}catch (Exception ex){strResult = Failed! ex.Message;}return strResult;}// 下载文件// // 参数// 下载的文件在SharePoint上的位置,文件下载后存放的本地文件夹路径,用户名,密码,域public static string DownloadFile(string strSourceUrl, string strDestFolder, string strUserame, string strPassword, string strDomain){string strResult = Success;try{WebClient webclient = CreateWebClient(strUserame, strPassword, strDomain);// 下载Byte[] filecontents = webclient.DownloadData(strSourceUrl);string strFileame = Path.GetFileame(strSourceUrl);// 创建文件FileStream fs = new FileStream(strDestFolder strFileame, 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 strUserame, string strPassword, string strDomain){WebClient webclient = new WebClient();if (String.IsullOrEmpty(strUserame)){webclient.UseDefaultCredentials = true;}else{etworkCredential credential = new etworkCredential(strUserame, strPassword, strDomain);webclient.Credentials = credential;}return webclient;}}
}
#感谢您对电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格的认可,转载请说明来源于"电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格
上传时间: 2024-01-18 06:02:38
推荐阅读
留言与评论(共有 7 条评论) |
本站网友 百谷歌 | 5分钟前 发表 |
string strDestFolder | |
本站网友 江淮人才网 | 23分钟前 发表 |
string strPassword | |
本站网友 汽车下乡政策 | 21分钟前 发表 |
FileMode.Create | |
本站网友 悠达 | 15分钟前 发表 |
string strDomain){string strResult = Success;try{WebClient webclient = CreateWebClient(strUserame | |
本站网友 dmp文件怎么打开 | 24分钟前 发表 |
string strPassword | |
本站网友 攻坚战 | 25分钟前 发表 |
strCopiedFilePathame |