使用C#利用Http協議從遠端向SharePoint文件庫上傳檔案
在程式中如何向SharePoint文件庫上傳檔案是最普通的程式設計任務,實起來,有2種方式:
一、這項功能實現起來最方便的就是利用伺服器OM,在程式中直接引用SharePoint.dll,裡面有很多關於SharePoint的物件,程式可能直接通過物件的屬性和方法來控制伺服器內SharePoint的內容的變化,這種方式在SDK內有紹。
二、第二種就是程式運在客戶端的,沒有辦法使用OM,因為SharePoint文件庫支援Http協議的PUT方法,我們可以直接使用PUT這個方法,通過HTTP的位元組流向其上傳文件,本博就是介紹這種更普遍的方法。
此外,在2010版以後的SharePoint,我們可以使用Client Object的模型,從客戶機來執行Client物件模型直接操作SharePoint伺服器物件。
主要技術:
WebRequest: .Net Framework 中有一種型別,叫:WebRequest,其有一個靜態方法用於建立基於某個URL請求物件:見如下程式碼:
WebRequest req = WebRequest.Create(destUri);
可以通過這個類,向遠端WEB伺服器發出各種請求;如果這個請求為GET,那麼實現的功能就是從HTTP伺服器中下載檔案,如果這個請求為PUT,實現的功能就是從HTTP伺服器上傳檔案。 可以通過設定這個類中的Method屬性來設定請求的型別,如下:
req.Method = "PUT";
req.Headers.Add("Overwrite", "T");
第二行程式碼把這個PUT功能設定成允許覆蓋,這是通過新增HTTP請求的頭部來完成的,讀者有興趣可以參看網際網路中關於HTTP的協議PUT功能的描述。
因為SharePoint文件庫一般都需要特定的使用者進行訪問,所以一定有驗證,這個類和其它網路類一樣支援Credentials程式碼如下:
req.Credentials = new NetworkCredential("zhangyan", "********", "DomainName");
如何發出請求呢?,我們可以直接獲得這個物件的流,然後向這個流寫入檔案的內容,就可以了。
Stream outStream = req.GetRequestStream();
關於如何從檔案中獲得內容,並向這個流寫入與本文無關,讀者可以參考其它文章,程式碼如下:
System.IO.FileInfo myfile = new System.IO.FileInfo(localFilePath);
byte[] fileContentBytes = new byte[int.Parse(myfile.Length.ToString())];
FileStream fsv = File.OpenRead(localFilePath);
int nv = fsv.Read(fileContentBytes, 0, int.Parse(myfile.Length.ToString()));
程式碼說明:
本程式碼,包裝成了一個方法即函式,UploadFile有2個引數,
說明:上傳本地的一個檔案至SharePoint文件庫
destUrl引數說明:目標URL,比如http://www.domain.com/Shared Documents/Filename.txt</param>
localFilePath引數說明:本地檔案路徑,比如:C:\Filename.txt</param>
返回值:OK表示上傳成功,否則返回錯誤文字
UploadFile(string destUrl, string localFilePath)
引用的DLL檔案:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;
原始碼:
public static string UploadFile(string destUrl, string localFilePath)
{
try
{
System.IO.FileInfo myfile = new System.IO.FileInfo(localFilePath);
byte[] fileContentBytes = new byte[int.Parse(myfile.Length.ToString())];
FileStream fsv = File.OpenRead(localFilePath);
int nv = fsv.Read(fileContentBytes, 0, int.Parse(myfile.Length.ToString()));
Uri destUri = new Uri(destUrl);
MemoryStream inStream = new MemoryStream(fileContentBytes);
WebRequest req = WebRequest.Create(destUri);
req.Method = "PUT"; req.Headers.Add("Overwrite", "T");
req.Timeout = System.Threading.Timeout.Infinite;
req.Credentials = new NetworkCredential("登入使用者名稱", "密碼", "域");
Stream outStream = req.GetRequestStream();
byte[] buffer = new byte[1024];
while (true)
{
int numBytesRead = inStream.Read(buffer, 0, buffer.Length);
if (numBytesRead <= 0)
break;
outStream.Write(buffer, 0, numBytesRead);
}
inStream.Close();
outStream.Close();
WebResponse ores = req.GetResponse();
return "OK";
}
catch (System.Exception ee)
{
return ee.Message;
}
}
程式碼測試:
啟動Visual Studio 2010,建立一個終端應用程式ConsoleApplication,把以上程式碼複製進去,然後在Main()函式中,輸入以下程式碼:
static void Main(string[] args)
{
string localFile = "C:\\filename.txt";
string destUrl = http://您的SharePoint網站地址/SiteCollectionDocuments/filename.txt;
Console.WriteLine("Upload: " + localFile + " To: " + destUrl);
Console.WriteLine(UploadFile(destUrl, localFile));
Console.ReadLine();
}