2

我正在构建一个 Windows 应用商店应用程序,我必须在其中从 SharePoint 2013 站点上传和下载图像。我尝试了 REST API,但我得到的只是包含文件属性的 XML 数据,但我无法获取实际文件。有人可以帮我吗?

以下是我正在尝试的代码:

HttpClientHandler handler = new HttpClientHandler();
handler.UseDefaultCredentials = false;
handler.Credentials = new NetworkCredential(userName, password, domain);

HttpClient client = new HttpClient(handler);
client.DefaultRequestHeaders.Add("Accept", "application/atom+xml");
client.DefaultRequestHeaders.Add("ContentType", "application/atom+xml;type=entry");

var response = await client.GetAsync("server/site/_api/web/ListName/Items(1)/File");
Byte[] bArray = await response.Content.ReadAsByteArrayAsync();
4

1 回答 1

2

以下示例演示如何从库中检索特定文件内容:

url: http://site url/_api/web/lists/getbytitle('<list title>')/items(<item id>)/File/$value
method: GET
headers:
    Authorization: "Bearer " + accessToken

例子

HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);

var response = client.GetAsync("https://tenant.sharepoint.com/_api/web/lists/getbytitle('Documents')/items(1)/File/$value", HttpCompletionOption.ResponseHeadersRead).Result;
var fileContent = response.Content.ReadAsByteArrayAsync().Result;

参考

使用 REST 处理文件夹和文件

于 2015-02-04T23:37:11.520 回答