[C#.NET][Sharepoint 2013] 如何使用 REST API 上传文件 for X-RequestDigest header

2021-02-04 18:15

阅读:486

标签:BLB   rtb   down   eth   lvf   end   上传   epo   aam   

[C#.NET][Sharepoint 2013] 如何使用 REST API 上传文件 for X-RequestDigest header


续上篇,http://www.dotblogs.com.tw/yc421206/archive/2014/06/12/145512.aspx

本文章节

  • 准备工作
  • 取得 FormDigestValue for? Advanced REST client
  • 取得 FormDigestValue for? C#
  • 上传文件 for C#

准备工作

根据下图,新增文件需要:

1.URI 为 http://sps2013/CsomApi/_api/web/GetFolderByServerRelativeUrl(‘Shared%20Documents‘)/Files/add(url=‘myDoc.docx‘,overwrite=true)

2.使用 HttpMethod.Post

3.根据 http://msdn.microsoft.com/en-us/library/office/jj164022%28v=office.15%29.aspx#bk_requestElements 所述 ,使用 OAuth 选 Authentication header ;反之选 X-RequestDigest header,本篇使用 X-RequestDigest header

下图出自,http://zoom.it/zdjx#full

技术图片


取得 FormDigestValue for? Advanced REST client

URI 为 http://sps2013/_api/contextinfo

技术图片

技术图片

取得 FormDigestValue for? C#

整个数据取得的方式并不困难,这里使用了Linq to Json (Json.NET from NuGet)将 FormDigestValue 拿出来


{
    HttpClientHandler httpClientHandler = new HttpClientHandler()
    {
        Credentials = new NetworkCredential(YourId, YourPassword, YourDomain)
    };

    using (var httpClient = new HttpClient(httpClientHandler))
    {
        httpClient.BaseAddress = new Uri("http://sps2013");
        httpClient.DefaultRequestHeaders.Add("Accept", "application/json;odata=verbose");
        HttpResponseMessage response = httpClient.PostAsync("/_api/contextinfo", null).Result;
        HttpRequestMessage a = new HttpRequestMessage(); a.Method = HttpMethod.Post;
        if (response.StatusCode == HttpStatusCode.OK)
        {
            var result = response.Content.ReadAsStringAsync().Result;

            JObject jsonObject = JObject.Parse(result);
            var formDigestValue = jsonObject.Descendants()
                .OfType().First(p => p.Name == "FormDigestValue")
            .Value;
            return formDigestValue.ToString();
        }

        return null;
    }
}

上传文件 for C#

最重要的是要带入 X-RequestDigest header


{
    HttpClientHandler httpClientHandler = new HttpClientHandler()
    {
        Credentials = new NetworkCredential(YourId, YourPassword, YourDomain)
    };
    using (var httpClient = new HttpClient(httpClientHandler))
    {
        httpClient.BaseAddress = new Uri("http://sps2013/CsomApi");
        httpClient.DefaultRequestHeaders.Add("Accept", "application/json;odata=verbose");
        httpClient.DefaultRequestHeaders.Add("X-RequestDigest", GetFormDigestValue());

        FileStream uploadStream = new FileStream("myDoc.docx", FileMode.Open, FileAccess.Read);
        HttpContent uploadContent = new StreamContent(uploadStream);
        HttpResponseMessage response = httpClient.PostAsync(
            "/_api/Web/GetFolderByServerRelativeUrl(‘Shared%20Documents‘)/Files/add(url=‘myDoc.docx‘,overwrite=true)",
            uploadContent).Result;
        if (response.StatusCode == HttpStatusCode.OK)
        {
            var result = response.Content.ReadAsStringAsync().Result;
            Console.WriteLine(result);
        }
        Console.WriteLine("successful down file,please press any key contiune");
        Console.ReadKey();
    }
}

本文出自:http://www.dotblogs.com.tw/yc421206/archive/2014/06/12/145518.aspx

若有谬误,烦请告知,新手发帖请多包涵

技术图片

技术图片2010~2017 C# 第四季

原文:大专栏  [C#.NET][Sharepoint 2013] 如何使用 REST API 上传文件 for X-RequestDigest header


[C#.NET][Sharepoint 2013] 如何使用 REST API 上传文件 for X-RequestDigest header

标签:BLB   rtb   down   eth   lvf   end   上传   epo   aam   

原文地址:https://www.cnblogs.com/petewell/p/11457854.html


评论


亲,登录后才可以留言!