如何使用WebService上传文件至SharePoint 文档库中(已解决)

问题描述: 通过WebService的形式将文件上传至SharePoint的文档库,我认为有2个难点,其中比较好解决的是文件传输,这个文件传输有好多种办法,比如使用ftp协议.最难的一个可能就是权限问题.(原来我的解决方法是正确的,只是模拟用户的时候域名写的不对,哈哈,)

我想会有好多人也会遇到这个问题,我的这个解决方法,在一定程度上可以解决一些问题.(^_^),关于身份模拟请参考msdn中的描述。此程序片段不包含目录验证的代码.

这个问题困扰了我好久.我曾经尝试过多种方法.但一样的程序,在windows应用程序里运行就ok,一放到webservice里就出问题了.(不过今天晚上又测试了多次,证明我的方法是有效的,兴奋中....)

以下是通过文件监控的方式实现的文件上传程序代码:(里面采用了模拟windows用户的方式模拟当前的操作用户)

  private void fileSystemWatcher1_Created(object sender, System.IO.FileSystemEventArgs e)
  {  
   Application.DoEvents();
   WindowsImpersonationContext wic = null;   
   if( wic != null ) wic.Undo();
   try
   {     
    System.IO.File.Copy(e.FullPath,"c://AresTemp//"+e.Name,true);

    FileStream fStream;    
    fStream= File.OpenRead("c://AresTemp//"+e.Name);   
    byte[] contents = new byte[fStream.Length];    
    fStream.Read(contents, 0, (int)fStream.Length); 
    fStream.Close();
    if(File.Exists("c://AresTemp//"+e.Name)==true)
    {
     File.Delete("c://AresTemp//"+e.Name);
    }         
    
    wic =SPUtil.GetWindowsIdentity();
    SPWeb myweb=new SPSite(SharePointURL).OpenWeb();
    myweb.AllowUnsafeUpdates=true;
    
    myweb.Files.Add(SharePointURL+"/"+e.Name,contents,true);
//    if (File.Exists(e.FullPath))
//     File.Delete(e.FullPath); 
    if( wic != null ) wic.Undo(); 
    AresLog.recordLog("文件上传成功","","",e.Name  ,e.FullPath);
    
   }
   catch(Exception ee)
   {
    if( wic != null ) wic.Undo();
    AresLog.recordLog("上传文件异常","","",ee.Message,ee.StackTrace);
   }
  }

以下是我写的通过ftp的方式,将文件传输到本地,然后读取本地磁盘文件,上传到sharpoint文档库的代码:(需要注意域名的书写,此代码已经过本机测试成功,但具体环境不同,也可能造成一定的差异)

 [WebMethod]
  public string UpLoadFilesToSPS(string FtpServer,string FtpUser,string FtpPwd,string FtpPath,string FilesName,string FilesInfo,string targetUrl)
  {
   //ftp settings
   FtpClient myFTP=new FtpClient();
   myFTP.Server=FtpServer;
   myFTP.Username=FtpUser;
   myFTP.Password=FtpPwd;
   myFTP.RemotePath=FtpPath;
   //filePath,info
   string[] fName=Microsoft.VisualBasic.Strings.Split(FilesName,";",-1,Microsoft.VisualBasic.CompareMethod.Text);
   string[] fInfo=Microsoft.VisualBasic.Strings.Split(FilesInfo,";",-1,Microsoft.VisualBasic.CompareMethod.Text);
   int i=0;
   //检查和创建本地环境
   if(Directory.Exists("C://AresTemp")==false)
   {
    Directory.CreateDirectory("C://AresTemp");
   }
   foreach(string fDirectory in fInfo)
   {
    if(Directory.Exists("C://AresTemp//"+fDirectory)==false)
    {
     Directory.CreateDirectory("C://AresTemp//"+fDirectory);
    }
   }
   //文件下载
   for(i=0;i<fName.Length;i++)
   {
    myFTP.DownloadEx(fName[i],"C://AresTemp//"+fInfo[i]+"//"+fName[i],true);
   }
   myFTP.Close();
  WindowsImpersonationContext wic = null; 
   
   if( wic != null ) wic.Undo();
   //获取模拟用户信息
   ZSoft.WindowsImpersonation.WIUser myUser=new ZSoft.WindowsImpersonation.WIUser(ServerUser,ServerName,ServerPassword);
   myUser.impersonateValidUser();
   wic =myUser.wic;
   SPWeb myweb=new SPSite(targetUrl).OpenWeb();
   myweb.AllowUnsafeUpdates=true; 

   for(i=0;i<fName.Length;i++)
   { 
    FileStream fStream;    
    fStream= File.OpenRead("c://AresTemp//"+fInfo[i]+"//"+fName[i]);   
    byte[] contents = new byte[fStream.Length];    
    fStream.Read(contents, 0, (int)fStream.Length); 
    fStream.Close();       
    myweb.Files.Add(targetUrl+"/"+fInfo[i]+"//"+fName[i],contents,true);  
   }
   myweb.Close();
   if( wic != null ) wic.Undo();
   return "文件传输成功!";
  }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值