I am trying to download a file from Sharepoing using ajax call from html page. I am able to upload successfully but when I try to download a file, in case of text file, the download is successfull and file opens with correct content, but in case of pdf, the file is downloaded as blank and in case of ppt, the file is corrput.
I am using below code. For now, I am hardcoding everything to get a successfull file. Please let me know what i may be missing.
<apex:page controller="XXX_SharePoint" showHeader="false" sidebar="false" showChat="false" docType="html-5.0" standardStylesheets="false" action="{!onPageLoad}">
<script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.9.1.min.js" type="text/javascript"></script>
<Script>
window.onload = () => {
let code = '{!ACCESS_TOKEN}';
let extension = '{!extension}';
//make a ajax callout
$.ajax({
url: "https://m.sharepoint.com/_api/Web/GetFileByServerRelativePath(decodedurl='/XXXDocument/pdf4.pdf')/$value",
type: 'get',
contentType: true,
processData: false,
headers: { accept: 'application/json',
"Authorization": "Bearer "+code,
},
success: function(response){
alert('success response'+response)
var a = document.createElement('a');
var binaryData = [];
binaryData.push(response);
var url = window.URL.createObjectURL(new Blob(binaryData),{type: "application/octet-stream"});
//var url = window.URL.createObjectURL(response.blob);
a.href = url;
a.download = 'myfile.pdf';//+extension;
document.body.appendChild(a);
a.click();
a.remove();
window.URL.revokeObjectURL(url);
},
});
}
</Script>
</apex:page>
type: 'get', contentType: true, processData: false, binaryStringResponseBody:true,