通过C# 应用程序往SharePoint文档库上传文件

本文介绍了一种使用C#应用程序将本地文件上传到SharePoint的方法。通过创建PUT Web请求,并利用FileStream读取本地文件内容,将其写入到请求流中实现文件上传。

今天发现了一个通过C#应用程序往SharePoint上传文件的方法。如下

1 static void Main(string[] args)
2 {
3 string _UploadedFilePath = "C:\\Documents and Settings\\bux\\Desktop\\TEST.PDF";
4 string _SharePointPath = "https://external1.collaboration.hp.com/external/RnD_Services/PRT/Job%20Proposal/JR.PDF";
5 UploadFileToSharePoint(_UploadedFilePath, _SharePointPath);
6 }
7
8 static void UploadFileToSharePoint(string UploadedFilePath, string SharePointPath)
9 {
10 WebResponse response = null;
11
12 try
13 {
14 // Create a PUT Web request to upload the file.
15 WebRequest request = WebRequest.Create(SharePointPath);
16
17 request.Credentials = CredentialCache.DefaultCredentials;
18 request.Method = "PUT";
19
20 // Allocate a 1 KB buffer to transfer the file contents.
21 // You can adjust the buffer size as needed, depending on
22 // the number and size of files being uploaded.
23 byte[] buffer = new byte[1024];
24
25 // Write the contents of the local file to the
26 // request stream.
27 using (Stream stream = request.GetRequestStream())
28 using (FileStream fsWorkbook = File.Open(UploadedFilePath,
29 FileMode.Open, FileAccess.Read))
30 {
31 int i = fsWorkbook.Read(buffer, 0, buffer.Length);
32
33 while (i > 0)
34 {
35 stream.Write(buffer, 0, i);
36 i = fsWorkbook.Read(buffer, 0, buffer.Length);
37 }
38 }
39
40 // Make the PUT request.
41 response = request.GetResponse();
42 }
43 catch (Exception ex)
44 {
45 throw ex;
46 }
47 finally
48 {
49 response.Close();
50 }
51 }

转载于:https://www.cnblogs.com/buxiaoyang/archive/2011/05/17/how_to_upload_file_to_SharePoint.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值