-1

我对这个共享点的东西真的很陌生。我们有一个带有管理员帐户的共享点服务器,我通过 ip 和手动共享点端口从我的本地计算机连接它。坚果我需要编写一个程序,该程序需要将文件从本地机器上传到共享点服务器到服务器。可以使用winforms吗?还是只能在网络服务中使用。?

   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();
}

}

尝试使用上面的代码,我从以下行收到错误

使用 (SPSite oSite = 新 SPSite(sharePointSite))

错误是

“无法找到位于http://server:port/的 Web 应用程序。验证您是否正确键入了 URL。如果 URL 应服务于现有内容,系统管理员可能需要将新的请求 URL 映射添加到预期应用"

并且无法上传文件。但是,如果我将相同的 URL 复制并粘贴到本地计算机中,我就可以访问部署在服务器中的共享点,甚至可以从本地计算机手动上传文件。

如何从与 LAN 连接的本地计算机上传 sharepoint 服务器中的文件..?

4

2 回答 2

1

siteURL = 共享点的主 URL(例如)“ http://10.0.0.14:48487/ ”;

documentListName = shrepoint 中的任何文件夹(例如)共享文档

documentName = 文件名(例如)sampleword.docx、readme.txt 等

documentStream = 我们要上传的文件的字节格式。

(例如)byte[] bytefile = System.IO.File.ReadAllBytes(filepath+filename);

public static void UploadDocument(string siteURL, string documentListName, string documentListURL,string documentName, byte[] documentStream = null)
    {  

     try
        {
        using (SP.ClientContext clientContext = new SP.ClientContext(siteURL))
        {

            #region"Only if you have credentials"
            NetworkCredential Cred = new NetworkCredential("username", "password");
            clientContext.Credentials = Cred;
            #endregion


            SP.List documentsList = clientContext.Web.Lists.GetByTitle(documentListName);

            var fileCreationInformation = new SP.FileCreationInformation();
            //Assign to content byte[] i.e. documentStream

            fileCreationInformation.Content = documentStream;
            //Allow owerwrite of document

            fileCreationInformation.Overwrite = true;
            //Upload URL

            fileCreationInformation.Url = documentName;

            Microsoft.SharePoint.Client.File uploadFile = documentsList.RootFolder.Files.Add(
                fileCreationInformation);


            uploadFile.ListItemAllFields.Update();
            clientContext.ExecuteQuery();

        }
    }
    catch (Exception ex)
    {
    }
}

}

这对我来说很完美:) :)

于 2015-08-26T12:56:18.577 回答
0

确保您连接到现有网站集。您收到的错误是非常自我解释的,它找不到您指向的网站集。检查您的字符串sharePointSite以确保没有拼写错误并且它正在访问正确的根网站集。请记住 SharePoint SPSite = 网站集 SPWeb = 网站集中的网站。

如果我自己运行一些东西,我看不到你的代码中有任何明显的错误,除非你在调用 oSite.openweb() 时它是针对网站集中的单个网站的。

于 2015-08-21T05:42:50.350 回答