使用 MS Graph API SharePoint 放置 pdf 文件时出错

问题描述 投票:0回答:1

我使用 MS Graph API 在 SharePoint 中上传本地文件。

它适用于 Word 或文本文件。我可以将它们上传到 SharePoint

我也可以上传 Pdf 文件,但无法在 SharePoint 中正确打开 Pdf:“此文件没有可用的预览”。 当然,如果我在SharePoint中手动上传Pdf,它会正常打开。

根据我在互联网上读到的内容,这似乎是一个编码问题,但我无法使其工作。 如何使用 MicrosoftGraph 将 pdf 文件上传到 Sharepoint 网站

我使用这个请求:

PUT /站点/{站点 ID}/驱动器/项目/{父 ID}:/{文件名}:/内容

Ex: Invoke-WebRequest -Method PUT -Uri "https: XXX sharepoint-online/v1/sites/siteid/drive/items/parentid:/PDFfile:/content" -Headers @{'APIKEY'='XXX';' Content-Type'='multipart/form-data';'Accept'='application/json';'Authorization'="Bearer accesstoken"} -InFile $pathtopdffile

有什么帮助/建议吗?

api pdf sharepoint graph
1个回答
0
投票

对于大小小于或等于 250MB 的文件,SharePoint 的标准端点为:

PUT /sites/{site-id}/drive/items/{item-id}/content

https://learn.microsoft.com/en-us/graph/api/driveitem-put-content?view=graph-rest-1.0&tabs=http

我已经使用HttpClient来调用这个端点:

{
    string uri;
    if (!string.IsNullOrEmpty(itemId)) // replace file
    {
        uri = $"https://graph.microsoft.com/v1.0/sites/{siteId}/drive/items/{itemId}/content";
    }
    else // new file
    {
        uri = $"https://graph.microsoft.com/v1.0/sites/{siteId}/drive/items/{parentId}:/{fileName}:/content";
    }
    
    var httpRequestMessage = new HttpRequestMessage(HttpMethod.Put, uri);
    
    httpRequestMessage.Content = new StreamContent(memoryStream);
    httpRequestMessage.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
    
    var httpResponseMessage = await _httpClient.SendAsync(httpRequestMessage);
    
    if (httpResponseMessage.IsSuccessStatusCode)
    {
        item = await httpResponseMessage.Content.ReadFromJsonAsync<DriveItem>();        
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.