首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用WebClient从SharePoint Online下载文件

使用WebClient从SharePoint Online下载文件
EN

Stack Overflow用户
提问于 2020-10-07 23:40:08
回答 1查看 1.5K关注 0票数 2

我正在使用.net 4.8 WebClient和Microsoft.SharePointOnline.CSOM包,尝试从SharePoint online下载文件。

代码语言:javascript
运行
复制
const string username = "myusername@domain.com";
const string password = "mypassword";
const string url = "https://tenant.sharepoint.com/:b:/r/filepath.pdf";
var securedPassword = new SecureString();
foreach (var c in password.ToCharArray())
{
    securedPassword.AppendChar(c);
}
var credentials = new SharePointOnlineCredentials(username, securedPassword);
using (var client = new WebClient())
{
    client.Credentials = credentials;
    client.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f");
    client.DownloadFile(url, "C:\\temp\\test.pdf"); //this file path definitely exists
}

我得到的错误是401未授权。我使用相同的凭据注销并登录到SharePoint环境,以确保它们是正确的。

我已经尝试在头文件中添加几个不同的用户代理,看看这是否有帮助。

代码语言:javascript
运行
复制
client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
//or
client.Headers.Add("User-Agent: Other");

但这并没有什么不同。

这类似于Download File From SharePoint 365,它使用的代码看起来与我相同,但Download Document from SharePoint Online using c# webclient stopped working表示,某些方面发生了变化,这种形式的身份验证不再适用于连接到SharePoint online。然而,我找不到任何关于我们现在应该使用什么的最新文档。

EN

回答 1

Stack Overflow用户

发布于 2020-10-08 11:17:26

你可以参考官方文档提供的代码。

https://docs.microsoft.com/en-us/sharepoint/dev/sp-add-ins/working-with-folders-and-files-with-rest

在我测试的时候,它工作得很好。

代码语言:javascript
运行
复制
public static void DownloadFileViaRestAPI(string webUrl, ICredentials credentials, string documentLibName, string fileName, string path)
        {
            webUrl = webUrl.EndsWith("/") ? webUrl.Substring(0, webUrl.Length - 1) : webUrl;
            string webRelativeUrl = null;
            if (webUrl.Split('/').Length > 3)
            {
                webRelativeUrl = "/" + webUrl.Split(new char[] { '/' }, 4)[3];
            }
            else
            {
                webRelativeUrl = "";
            }

            using (WebClient client = new WebClient())
            {
                client.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f");
                client.Credentials = credentials;
                Uri endpointUri = new Uri(webUrl + "/_api/web/GetFileByServerRelativeUrl('" + webRelativeUrl + "/" + documentLibName + "/" + fileName + "')/$value");

                byte[] data = client.DownloadData(endpointUri);
                FileStream outputStream = new FileStream(path + fileName, FileMode.OpenOrCreate | FileMode.Append, FileAccess.Write, FileShare.None);
                outputStream.Write(data, 0, data.Length);
                outputStream.Flush(true);
                outputStream.Close();
            }
        }

        static void Main(string[] args)
        {
            string siteURL = "https://contoso.sharepoint.com/sites/dev";

            //set credential of SharePoint online
            SecureString secureString = new SecureString();
            foreach (char c in "password".ToCharArray())
            {
                secureString.AppendChar(c);
            }
            ICredentials credentials = new Microsoft.SharePoint.Client.SharePointOnlineCredentials("amos@contoso.onmicrosoft.com", secureString);

            //set credential of SharePoint 2013(On-Premises)
            //string userName = "Administrator";
            //string password = "xxxxxxx";
            //string domain = "CONTOSO";
            //ICredentials credentials = new NetworkCredential(userName, password, domain);

            DownloadFileViaRestAPI(siteURL, credentials, "lib", "test.css", "c:\\SPFX\\");

            Console.WriteLine("success");
            Console.ReadLine();
        }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64247545

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档