SharePoint 提供了强大的文档管理功能,能够创建各种类型的文档库,并对文档进行相应的管理。但是,对于用户来说,每次上传下载文档都要登录到网点,稍嫌麻烦,遇到网速慢的时候,更为恼火。那么能否用客户端的代码上传或下载文档呢?答案是肯定的,下面的一段代码就是在本地创建一个Http Client程序,来处理服务器文档库文件。
using
System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Web;
using System.Net;
using System.Data;
using System.Diagnostics;
namespace com.sinoair.local
{
public static class FileHelper
{
public static string UploadFile( string destUrl, string content)
{
try
{
Uri destUri = new Uri(destUrl);
MemoryStream inStream = new MemoryStream(Encoding.UTF8.GetBytes(content));
WebRequest req = WebRequest.Create(destUri); req.Method = " PUT " ;
req.Headers.Add( " Overwrite " , " T " );
req.Timeout = System.Threading.Timeout.Infinite;
req.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
Stream outStream = req.GetRequestStream();
string status = CopyStream(inStream, outStream);
if (status == " success " )
{
outStream.Close();
WebResponse ores = req.GetResponse();
return " success " ;
}
else
{
return status;
}
}
catch (WebException we)
{
return we.Message;
}
catch (System.Exception ee)
{
return ee.Message;
}
}
public static string DownloadFile( string sourceUrl, string destFolder)
{
try
{
System.Uri sourceUri = new System.Uri(sourceUrl);
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(sourceUri);
req.Method = " GET " ;
req.Timeout = System.Threading.Timeout.Infinite;
req.Credentials = CredentialCache.DefaultCredentials;
req.AllowAutoRedirect = true ; WebResponse res = req.GetResponse();
Stream inStream = res.GetResponseStream();
FileStream fs = new FileStream(destFolder, FileMode.OpenOrCreate);
string status = CopyStream(inStream, fs);
if (status == " success " )
{
inStream.Close();
fs.Close();
fs.Dispose();
return " success " ;
}
else
{
inStream.Close();
fs.Close();
fs.Dispose();
return status;
}
}
catch (WebException we)
{
return we.Message;
}
catch (System.Exception ee)
{
return ee.Message;
}
}
private static string CopyStream(Stream inStream, Stream outStream)
{
try
{
byte [] buffer = new byte [ 1024 ];
for (; ; )
{
int numBytesRead = inStream.Read(buffer, 0 , buffer.Length);
if (numBytesRead <= 0 )
break ;
outStream.Write(buffer, 0 , numBytesRead);
}
inStream.Close();
return " success " ;
}
catch (System.Exception ee)
{
return ee.Message;
}
}
}
}
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Web;
using System.Net;
using System.Data;
using System.Diagnostics;
namespace com.sinoair.local
{
public static class FileHelper
{
public static string UploadFile( string destUrl, string content)
{
try
{
Uri destUri = new Uri(destUrl);
MemoryStream inStream = new MemoryStream(Encoding.UTF8.GetBytes(content));
WebRequest req = WebRequest.Create(destUri); req.Method = " PUT " ;
req.Headers.Add( " Overwrite " , " T " );
req.Timeout = System.Threading.Timeout.Infinite;
req.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
Stream outStream = req.GetRequestStream();
string status = CopyStream(inStream, outStream);
if (status == " success " )
{
outStream.Close();
WebResponse ores = req.GetResponse();
return " success " ;
}
else
{
return status;
}
}
catch (WebException we)
{
return we.Message;
}
catch (System.Exception ee)
{
return ee.Message;
}
}
public static string DownloadFile( string sourceUrl, string destFolder)
{
try
{
System.Uri sourceUri = new System.Uri(sourceUrl);
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(sourceUri);
req.Method = " GET " ;
req.Timeout = System.Threading.Timeout.Infinite;
req.Credentials = CredentialCache.DefaultCredentials;
req.AllowAutoRedirect = true ; WebResponse res = req.GetResponse();
Stream inStream = res.GetResponseStream();
FileStream fs = new FileStream(destFolder, FileMode.OpenOrCreate);
string status = CopyStream(inStream, fs);
if (status == " success " )
{
inStream.Close();
fs.Close();
fs.Dispose();
return " success " ;
}
else
{
inStream.Close();
fs.Close();
fs.Dispose();
return status;
}
}
catch (WebException we)
{
return we.Message;
}
catch (System.Exception ee)
{
return ee.Message;
}
}
private static string CopyStream(Stream inStream, Stream outStream)
{
try
{
byte [] buffer = new byte [ 1024 ];
for (; ; )
{
int numBytesRead = inStream.Read(buffer, 0 , buffer.Length);
if (numBytesRead <= 0 )
break ;
outStream.Write(buffer, 0 , numBytesRead);
}
inStream.Close();
return " success " ;
}
catch (System.Exception ee)
{
return ee.Message;
}
}
}
}
假定我们有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 即可。