SharePoint 提供了强大的文档管理功能,能够创建各种类型的文档库,并对文档进行相应的管理。但是,对于用户来说,每次上传下载文档都要登录到网点,稍嫌麻烦,遇到网速慢的时候,更为恼火。那么能否用客户端的代码上传或下载文档呢?答案是肯定的,下面的一段代码就是在本地创建一个Http Client程序,来处理服务器文档库文件。
1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4
using System.IO;
5
using System.Web;
6
using System.Net;
7
using System.Data;
8
using System.Diagnostics;
9
10
namespace com.sinoair.local
11
{
12
public static class FileHelper
13
{
14
public static string UploadFile(string destUrl, string content)
15
{
16
try
17
{
18
Uri destUri = new Uri(destUrl);
19
MemoryStream inStream = new MemoryStream(Encoding.UTF8.GetBytes(content));
20
WebRequest req = WebRequest.Create(destUri);
21
req.Method = "PUT";
22
req.Headers.Add("Overwrite", "T");
23
req.Timeout = System.Threading.Timeout.Infinite;
24
req.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
25
Stream outStream = req.GetRequestStream();
26
string status = CopyStream(inStream, outStream);
27
if (status == "success")
28
{
29
outStream.Close();
30
WebResponse ores = req.GetResponse();
31
return "success";
32
}
33
else
34
{
35
return status;
36
}
37
}
38
catch (WebException we)
39
{
40
return we.Message;
41
}
42
catch (System.Exception ee)
43
{
44
return ee.Message;
45
}
46
}
47
48
public static string UploadFile(string destUrl, string content, string username, string password)
49
{
50
try
51
{
52
Uri destUri = new Uri(destUrl);
53
MemoryStream inStream = new MemoryStream(Encoding.UTF8.GetBytes(content));
54
//FileStream inStream = File.OpenRead(sourcePath);
55
WebRequest req = WebRequest.Create(destUri);
56
req.Method = "PUT"; req.Headers.Add("Overwrite", "T");
57
req.Timeout = System.Threading.Timeout.Infinite;
58
req.Credentials = new NetworkCredential(username, password, "sinoair");
59
Stream outStream = req.GetRequestStream();
60
string status = CopyStream(inStream, outStream);
61
if (status == "success")
62
{
63
outStream.Close();
64
WebResponse ores = req.GetResponse();
65
return "success";
66
}
67
else
68
{
69
return status;
70
}
71
}
72
catch (WebException we)
73
{
74
return we.Message;
75
}
76
catch (System.Exception ee)
77
{
78
return ee.Message;
79
}
80
}
81
public static string DownloadFile(string sourceUrl, string destFolder)
82
{
83
try
84
{
85
System.Uri sourceUri = new System.Uri(sourceUrl);
86
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(sourceUri);
87
req.Method = "GET";
88
req.Timeout = System.Threading.Timeout.Infinite;
89
req.Credentials = CredentialCache.DefaultCredentials;
90
req.AllowAutoRedirect = true; WebResponse res = req.GetResponse();
91
Stream inStream = res.GetResponseStream();
92
FileStream fs = new FileStream(destFolder, FileMode.OpenOrCreate);
93
string status = CopyStream(inStream, fs);
94
if (status == "success")
95
{
96
inStream.Close();
97
fs.Close();
98
fs.Dispose();
99
return "success";
100
}
101
else
102
{
103
inStream.Close();
104
fs.Close();
105
fs.Dispose();
106
return status;
107
}
108
}
109
catch (WebException we)
110
{
111
return we.Message;
112
}
113
catch (System.Exception ee)
114
{
115
return ee.Message;
116
}
117
}
118
119
public static string DownloadFile(string sourceUrl, string destFolder, string username, string password)
120
{
121
try
122
{
123
System.Uri sourceUri = new System.Uri(sourceUrl);
124
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(sourceUri);
125
req.Method = "GET"; req.Timeout = System.Threading.Timeout.Infinite;
126
req.Credentials = new System.Net.NetworkCredential(username, password, "sinoair");
127
req.AllowAutoRedirect = true;
128
WebResponse res = req.GetResponse();
129
Stream inStream = res.GetResponseStream();
130
FileStream fs = new FileStream(destFolder, FileMode.OpenOrCreate);
131
string status = CopyStream(inStream, fs);
132
if (status == "success")
133
{
134
inStream.Close();
135
fs.Close();
136
fs.Dispose();
137
return "success";
138
}
139
else
140
{
141
inStream.Close();
142
fs.Close();
143
fs.Dispose();
144
return status;
145
}
146
}
147
catch (WebException we)
148
{
149
return we.Message;
150
}
151
catch (System.Exception ee)
152
{
153
return ee.Message;
154
}
155
}
156
157
private static string CopyStream(Stream inStream, Stream outStream)
158
{
159
try
160
{
161
byte[] buffer = new byte[1024];
162
for (; ; )
163
{
164
int numBytesRead = inStream.Read(buffer, 0, buffer.Length);
165
if (numBytesRead <= 0)
166
break;
167
outStream.Write(buffer, 0, numBytesRead);
168
}
169
inStream.Close();
170
return "success";
171
}
172
catch (System.Exception ee)
173
{
174
return ee.Message;
175
}
176
}
177
}
178
}
using System;2
using System.Collections.Generic;3
using System.Text;4
using System.IO;5
using System.Web;6
using System.Net;7
using System.Data;8
using System.Diagnostics;9

10
namespace com.sinoair.local11
{12
public static class FileHelper13
{14
public static string UploadFile(string destUrl, string content)15
{16
try17
{18
Uri destUri = new Uri(destUrl);19
MemoryStream inStream = new MemoryStream(Encoding.UTF8.GetBytes(content));20
WebRequest req = WebRequest.Create(destUri); 21
req.Method = "PUT";22
req.Headers.Add("Overwrite", "T");23
req.Timeout = System.Threading.Timeout.Infinite;24
req.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;25
Stream outStream = req.GetRequestStream();26
string status = CopyStream(inStream, outStream);27
if (status == "success")28
{29
outStream.Close();30
WebResponse ores = req.GetResponse();31
return "success";32
}33
else34
{35
return status;36
}37
}38
catch (WebException we)39
{40
return we.Message;41
}42
catch (System.Exception ee)43
{44
return ee.Message;45
}46
}47

48
public static string UploadFile(string destUrl, string content, string username, string password)49
{50
try51
{52
Uri destUri = new Uri(destUrl);53
MemoryStream inStream = new MemoryStream(Encoding.UTF8.GetBytes(content));54
//FileStream inStream = File.OpenRead(sourcePath); 55
WebRequest req = WebRequest.Create(destUri);56
req.Method = "PUT"; req.Headers.Add("Overwrite", "T");57
req.Timeout = System.Threading.Timeout.Infinite;58
req.Credentials = new NetworkCredential(username, password, "sinoair");59
Stream outStream = req.GetRequestStream();60
string status = CopyStream(inStream, outStream);61
if (status == "success")62
{63
outStream.Close();64
WebResponse ores = req.GetResponse();65
return "success";66
}67
else68
{69
return status;70
}71
}72
catch (WebException we)73
{74
return we.Message;75
}76
catch (System.Exception ee)77
{78
return ee.Message;79
}80
}81
public static string DownloadFile(string sourceUrl, string destFolder)82
{83
try84
{85
System.Uri sourceUri = new System.Uri(sourceUrl);86
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(sourceUri);87
req.Method = "GET";88
req.Timeout = System.Threading.Timeout.Infinite;89
req.Credentials = CredentialCache.DefaultCredentials;90
req.AllowAutoRedirect = true; WebResponse res = req.GetResponse();91
Stream inStream = res.GetResponseStream();92
FileStream fs = new FileStream(destFolder, FileMode.OpenOrCreate);93
string status = CopyStream(inStream, fs);94
if (status == "success")95
{96
inStream.Close();97
fs.Close();98
fs.Dispose();99
return "success";100
}101
else102
{103
inStream.Close();104
fs.Close();105
fs.Dispose();106
return status;107
}108
}109
catch (WebException we)110
{111
return we.Message;112
}113
catch (System.Exception ee)114
{115
return ee.Message;116
}117
}118

119
public static string DownloadFile(string sourceUrl, string destFolder, string username, string password)120
{121
try122
{123
System.Uri sourceUri = new System.Uri(sourceUrl);124
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(sourceUri);125
req.Method = "GET"; req.Timeout = System.Threading.Timeout.Infinite;126
req.Credentials = new System.Net.NetworkCredential(username, password, "sinoair");127
req.AllowAutoRedirect = true;128
WebResponse res = req.GetResponse();129
Stream inStream = res.GetResponseStream();130
FileStream fs = new FileStream(destFolder, FileMode.OpenOrCreate);131
string status = CopyStream(inStream, fs);132
if (status == "success")133
{134
inStream.Close();135
fs.Close();136
fs.Dispose();137
return "success";138
}139
else140
{141
inStream.Close();142
fs.Close();143
fs.Dispose();144
return status;145
}146
}147
catch (WebException we)148
{149
return we.Message;150
}151
catch (System.Exception ee)152
{153
return ee.Message;154
}155
}156

157
private static string CopyStream(Stream inStream, Stream outStream)158
{159
try160
{161
byte[] buffer = new byte[1024];162
for (; ; )163
{164
int numBytesRead = inStream.Read(buffer, 0, buffer.Length);165
if (numBytesRead <= 0)166
break;167
outStream.Write(buffer, 0, numBytesRead);168
}169
inStream.Close();170
return "success";171
}172
catch (System.Exception ee)173
{174
return ee.Message;175
}176
}177
}178
}假定我们有SharePoint服务器,名为portal,下面建了一个DocLib1的文档库,则该文档库的地址为http://portal/DocLib1 ,如果我们想将本地文件上传到文档库中,文件名为test.txt,即可调用FileHelper.UploadFile("http://portal/DocLib1/test.txt", "Text File Content")。对于文档库中的文件夹(例如Folder1),其对应的网址为http://portal/DocLib1/Folder1,则上传的参数相应改为http://portal/DocLib1/Folder1/test.txt 即可。


浙公网安备 33010602011771号