利用客户端向SharePoint文档库上传文档

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;
            }
        }
    }
}

 

假定我们有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 即可。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值