[英]How to Upload a Local File into sharepoint using Graph API?
I am trying to upload a local PDF file into share point via using Graph API but the PUT method is just letting me to create a new file in the SharePoint but not letting me upload a local existing C:/ Drive PDF file I am trying to upload a local PDF file into share point via using Graph API but the PUT method is just letting me to create a new file in the SharePoint but not letting me upload a local existing C:/ Drive PDF file
Here is the API that I am using这是我正在使用的 API
PUT https://graph.microsoft.com/v1.0/drives/ {drive-id}/root:/SharePointFilePath/file-name:/content放置 https://graph.microsoft.com/v1.0/drives/ {drive-id}/root:/SharePointFilePath/file-name:/content
As per here, that is the correct endpoint.根据这里,这是正确的端点。 however, you need to send ithe file as a binary stream in the body.
但是,您需要将文件作为二进制 stream 发送到正文中。 https://docs.microsoft.com/en-us/graph/api/driveitem-put-content?view=graph-rest-1.0&tabs=http
https://docs.microsoft.com/en-us/graph/api/driveitem-put-content?view=graph-rest-1.0&tabs=http
This means that you need to read your local pdf file into a binary stream and send it to the endpoint in the body of the put request.这意味着您需要将本地 pdf 文件读入二进制 stream 并将其发送到 put 请求正文中的端点。 as a test for example, you could create a regular text file with the body as string and contenttype as text.
例如,作为测试,您可以创建一个常规文本文件,其中正文为字符串,内容类型为文本。
also if it's larger than 4MB, then you need to use a different method.如果它大于 4MB,那么你需要使用不同的方法。 https://developer.microsoft.com/en-us/graph/docs/api-reference/v1.0/api/driveitem_createuploadsession mainly an uploadsession.
https://developer.microsoft.com/en-us/graph/docs/api-reference/v1.0/api/driveitem_createuploadsession主要是一个上传会话。
Previous test script to upload file to SharePoint by PnP PowerShell.以前的测试脚本通过 PnP PowerShell 将文件上传到 SharePoint。
https://social.technet.microsoft.com/Forums/msonline/en-US/25191b51-41dd-4f4b-93aa-a46594c9a184/uploading-a-file-to-sharepoint-online-using-microsoft-graph-api?forum=onlineservicessharepoint https://social.technet.microsoft.com/Forums/msonline/en-US/25191b51-41dd-4f4b-93aa-a46594c9a184/uploading-a-file-to-sharepoint-online-using-microsoft-graph-api?论坛=在线服务共享点
Connect-PnPOnline -AppId 'yourAzure app id' -AppSecret "yourAzure app secret" -AADDomain 'xxx.onmicrosoft.com'
$accessToken= Get-PnPAccessToken
$apiUrl = "https://graph.microsoft.com/v1.0/sites/xxx.sharepoint.com,x,x/drives/driveid/items/01AKXHS4ELSEOTLPZHZZDYYBOW57WR6HK6:/test.xlsx:/content"
$path="C:\Lee\Script\testdata.xlsx"
$file=[IO.File]::ReadAllBytes($path)
Invoke-RestMethod -Headers @{Authorization = "Bearer $accessToken"} -Uri $apiUrl -Method Put -Body $file -ContentType "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.