Quantcast
Channel: SharePoint 2013 - Development and Programming forum
Viewing all articles
Browse latest Browse all 7589

SharePoint 2013 Download file using REST

$
0
0

Hello All,

I want to download a file using REST API.

I have a file in a document library /folder. I want download that file, but with a different name.

Edit 1 : Below is my code. i am able to download file on Chrome browser but data gets corrupted and also not working on IE 10+

function call_to_rest_binarystring(rest_url)
{
    return $.ajax({
    url: rest_url,
    method: "GET",
});
}

function downloadFile(rest_url, filepath, fileName)
{
    var dfd = $.Deferred();
if (!window.ActiveXObject)
{
    var save = document.createElement('a');
    save.href = filepath;
    save.target = '_blank';
    save.download = fileName || 'unknown';

    var event = document.createEvent('Event');
    event.initEvent('click', true, true);
    save.dispatchEvent(event);
    (window.URL || window.webkitURL).revokeObjectURL(save.href);
    dfd.resolve(true);
}
else if (!!window.ActiveXObject && document.execCommand)
{
    var rest_return_call = call_to_rest_binarystring(rest_url)
    rest_return_call.done(function (response, status, xhr)
    {


        var blob = new Blob([response], { type: "application/" + fileName.split(".")[1] + "" });
        window.navigator.msSaveBlob(blob, fileName);

    });

}
return dfd.promise()
}

var rest_url = "<site name>/_api/web/GetFileByServerRelativeUrl('/ShareDocument/111.pdf')/openbinarystream"

downloadFile(rest_url, filepath, fileName)
while we download file it is downloaded as a blank. i think i am missing something. can anyone help me with this.



Viewing all articles
Browse latest Browse all 7589

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>