问题描述: 通过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 "文件传输成功!";
}