在客户端服务器应用程序中以编程方式将文件上载到SharePoint 2010中的文档库

在客户端服务器应用程序中以编程方式将文件上载到SharePoint 2010中的文档库,sharepoint,sharepoint-2010,Sharepoint,Sharepoint 2010,我正在使用以下代码在SharePoint 2010库中上载文件 String fileToUpload = @"C:\YourFile.txt"; String sharePointSite = "http://yoursite.com/sites/Research/"; String documentLibraryName = "Shared Documents"; using (SPSite oSite = new SPSite(sharePointSite)) {

我正在使用以下代码在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)

您是在控制台应用程序中运行此功能还是在SharePoint中运行此功能


可能是运行代码的帐户在C:\?

中没有读取权限吗?当您在服务器上运行代码时,您的代码在另一个没有读取C驱动器权限的帐户(apppool标识)下运行

我不知道你为什么要从同一台服务器上读取和上传文件,看起来你只是在测试Sharepoint对象模型,然后就可以了


如果您希望其他应用程序或服务为Sharepoint保留更新的文件,则应将其移动到web目录,即\wwwroot\wss\VirtualDirectories\80,然后使用您的代码读取和更新您的文档库(myLibrary)。

我正在使用Sharepoint应用程序。这与许可无关。从客户端计算机读取文件时出现问题。
String fileToUpload=@“C:\YourFile.txt”引用位于服务器上的文件。
字符串fileToUpload=@“C:\YourFile.txt”引用位于服务器上的文件。如果要将文件从本地计算机上载到SharePoint/服务器,则页面上需要有一个可以从硬盘读取数据的组件。例如,ASP.NET文件上载。有关示例,请参见。