从Sharepoint下载文件

用户名

我正在尝试从sharepoint下载文件,我有此代码并抛出错误代码500。

       static void DownloadFile(){
        string serverFilePath = "Here goes my URL, that open the file from any tab";
        var password = new SecureString();
        foreach (char c in Configuration.password) {
            password.AppendChar(c);
        }
        //  theese are the credentials and work fine because I tested in another method
        var o365credentials = new SharePointOnlineCredentials(Configuration.userName, password);

        var url = string.Format("{0}/{1}", Configuration.siteUrl, serverFilePath);
        //  My destination folder
        string destPath = @"C:\publisher";
        var request = System.Net.HttpWebRequest.Create(url);
        request.Credentials = o365credentials;
        using (var sReader = new StreamReader(request.GetResponse().GetResponseStream())) {
            using (var sWriter = new StreamWriter(destPath)) {
                sWriter.Write(sReader.ReadToEnd());
            }
        }
    }
乔纳森

您可以使用WebRequest完成此任务,以便从sharepoint网站下载文件:

public void DownloadFile(string serverFilePath, string destPath)
{
    var url = string.Format("{0}/{1}", ServerURL, serverFilePath);
    Directory.CreateDirectory(Path.GetDirectoryName(destPath)); // this method creates your directory
    var request = System.Net.HttpWebRequest.Create(url);
    request.Credentials = System.Net.CredentialCache.DefaultCredentials;
    using (var sReader = new StreamReader(request.GetResponse().GetResponseStream()))
    {
        using (var sWriter = new StreamWriter(destPath))
        {
            sWriter.Write(sReader.ReadToEnd());
        }
    }
}

如果您希望使用,则应Client-object-model阅读以下内容:如何使用仅带有绝对URL的SharePoint Client Object Model获取文件?

编辑:修复了CreateDirectory调用的拼写

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章