I am trying to download a file from Sharepoint using ajax call from Visual force 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>

        window.onload = () => {
            let code = '{!ACCESS_TOKEN}';   
            let extension = '{!extension}';

            //make a ajax callout
                url: "https://XXX.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 = [];
                    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;


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.