我使用下面的代码在SharePoint 2010库中上传文件
String fileToUpload = @"C:\YourFile.txt";
String sharePointSite = "http://yoursite.com/sites/Research/";
String documentLibraryName = "Shared Documents";
using (SPSite oSite = new SPSite(sharePointSite))
{
using (SPWeb oWeb = oSite.OpenWeb())
{
if (!System.IO.File.Exists(fileToUpload))
throw new FileNotFoundException("File not found.", fileToUpload);
SPFolder myLibrary = oWeb.Folders[documentLibraryName];
// Prepare to upload
Boolean replaceExistingFiles = true;
String fileName = System.IO.Path.GetFileName(fileToUpload);
FileStream fileStream = File.OpenRead(fileToUpload);
// Upload document
SPFile spfile = myLibrary.Files.Add(fileName, fileStream, replaceExistingFiles);
// Commit
myLibrary.Update();
}
}
这在我的机器上运行良好 . 但是,当我在服务器上部署它并使用下面的代码片段从我的机器上传库中的文件时,它会出错 . 它没有从本地(客户端)计算机获取文件位置(C:\ YourFile.txt) .
2 回答
当您在服务器上运行时,您的代码在不同的帐户(apppool身份)下运行,该帐户没有读取C驱动器的权限 .
我不知道你为什么要从同一台服务器上读取和上传文件,看起来你只是在测试Sharepoint对象模型然后就可以了
如果您希望其他应用或服务保留Sharepoint的更新文件,则应将其移至Web目录,即\ wwwroot \ wss \ VirtualDirectories \ 80,然后使用您的代码读取和更新您的doc lib(myLibrary)as你在干嘛
您是在控制台应用程序或“在SharePoint中”运行它吗?
可能是运行代码的帐户在C:\中没有读取权限?