简体   繁体   中英

Upload PDF to OneDrive Microsoft Graph Rest API

I would like to use JavaScript to upload a PDF file to my SharePoint Office 365 site. I have been able to - Authenticate and received Token - Use example to create a plain text file

I would like to upload a PDF file onto SharePoint. I have tried converting the file to binary and to base64 but the resultant file appears to be a text file.

var xhr = new ActiveXObject("Microsoft.XMLHTTP");
url = "https://graph.microsoft.com/beta/drive/root:/test10.pdf:/content"
xhr.open("PUT",url,false);
xhr.setRequestHeader("Authorization", "bearer" + " " + TOKEN);
//xhr.setRequestHeader("Content-Type","application/octet-stream");
xhr.setRequestHeader("Content-Type","application/pdf");
xhr.setRequestHeader("Content-Encoding","base64");
xhr.send(filecontent);

Is anyone able to help?

Thank you Terry

Found the issue. When reading the file in the content was set to text. I had to change the type to binary.

Bs.Type = 1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM