使用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();

        }

猜你喜欢

转载自blog.csdn.net/LanSeTianKong12/article/details/85169864