我在aspx文件中使用文件上传控件,在按钮点击时将单个文件上传到服务器。这还包含两种方法:1)将文件上载到SharePoint 2007库; 2)使用上传文件的路径和文件名更新sql数据库。
以下代码在本地开发计算机上的Visual Studio中进行调试时成功运行,或者如果我远程进入部署了它的开发服务器计算机。当我从客户端计算机浏览器运行Web应用程序时,上载到SharePoint失败的UploadFileToSharePoint()方法,但上传到本地并更新到db确实成功。
我不明白为什么它在本地运行但在部署后只能部分工作。我在服务器部署设置中遗漏了什么吗?我正在使用Windows身份验证。
Error Details:
Message: Object reference not set to an instance of an object.,
Source: App_Web_jybzycof,
TargetSite: Void UploadFileToSharePoint(System.String, System.String),
StackTrace: at ASP.fileupload_aspx.UploadFileToSharePoint(String UploadedFilePath, String SharePointPath) in c:\inetpub\wwwroot\webappTest\FileUpload.aspx:line 123 at ASP.fileupload_aspx.btnUploadFile_Click(Object sender, EventArgs e) in c:\inetpub\wwwroot\webappTest\FileUpload.aspx:line 64,
uploadedFilePath: C:\folder1\folder2\folder3\uploads\,
sharePointListPath: http://server/site/subsite/library
protected void btnUploadFile_Click(object sender, EventArgs e)
{
string strFilenameToUpload = string.Empty;
//check if file was selected to upload
if (FileUpload1.HasFile)
try
{
//get file name
strFilenameToUpload = FileUpload1.FileName;
//Save selected file into specified location
FileUpload1.SaveAs(uploadedFilePath + FileUpload1.FileName);
Label1.Text = "<br />Success! <br /> <br />" +
"File name: [" + FileUpload1.FileName + "] " + "was uploaded to <a href='http://server/site/subsite/library/'>SharePoint.</a>" +
"<br />" +
"File size: " + FileUpload1.PostedFile.ContentLength + " bytes<br />" +
"Content type: " + FileUpload1.PostedFile.ContentType;
//establish url for this upload
strDocumentUrlToUpload = sharePointListPath + "/" + FileUpload1.FileName;
UploadFileToSharePoint(
uploadedFilePath + FileUpload1.FileName,
sharePointListPath + FileUpload1.FileName);
}
catch (Exception ex)
{
Label1.Text = "ERROR Message: " + ex.Message.ToString() +
", Source: " + ex.Source +
", TargetSite: " + ex.TargetSite + ", StackTrace: " + ex.StackTrace +
", uploadedFilePath: " + uploadedFilePath +
", sharePointListPath: " + sharePointListPath;
}
else
{
Label1.Text = "You have not specified a file.";
}
//Save SharePoint path, filename to database
SqlConnLib updater = new SqlConnLib();
updater.UpdateFileinfo(strDocumentUrlToUpload, strFilenameToUpload);
}
protected void UploadFileToSharePoint(string UploadedFilePath,string SharePointPath)
{
WebResponse response = null;
try
{
// Create a PUT Web request to upload the file to SharePoint
WebRequest request = WebRequest.Create(SharePointPath);
request.Credentials = CredentialCache.DefaultCredentials;
request.Method = "PUT";
// Allocate a 1K buffer to transfer the file contents.
byte[] buffer = new byte[1024];
// Write the contents of the local file to the request stream.
using (Stream stream = request.GetRequestStream())
using (FileStream fsWorkbook = File.Open(UploadedFilePath,FileMode.Open, FileAccess.Read))
{
int i = fsWorkbook.Read(buffer, 0, buffer.Length);
while (i > 0)
{
stream.Write(buffer, 0, i);
i = fsWorkbook.Read(buffer, 0, buffer.Length);
}
}
// Make the PUT request.
response = request.GetResponse();
}
catch (Exception ex)
{
throw ex;
}
finally
{
response.Close();
}
}
</script>
更新:由于这没有提示任何回复 - 我会分享我从研究中学到的东西。我了解到解决方案是编写自定义Web服务以从远程服务器上传到SharePoint 2007 。有一个OOB SharePoint Web服务但它只能从一个SharePoint安装复制到另一个 - 它不能从远程(非SP安装)服务器工作到SP。客户端对象模型是一种可行的技术,但它不适用于SP 2007.(它应该适用于SP 2010和我假设2013)。通过Google搜索可以找到大量的解决方案/示例,但大多数都没有告诉您它们不能远程工作 - 它们只能在应用程序从安装它的同一服务器运行时才能工作。
目前,我将上传到服务器作为解决方案。然后在升级SharePoint安装后修改应用程序。