Download file with a REST request needing headers and giving the content

后端 未结 4 826
甜味超标
甜味超标 2021-02-06 08:58

I am using AngularJs with a REST API. I don\'t have the hand on the REST API. I can store digital object with the API by sending a REST request. I can get it also with a GET req

相关标签:
4条回答
  • 2021-02-06 09:20

    Thanks everybody for helping find a solution.

    I am not able to find a satisfying solution in javascript and client side application. I am going to make a proxy class communicating with the API.

    This will send the REST request with security headers and will give as response the file content.

    This class will be called by a HTTP GET request, so the 'save as' process will be managed easily thanks to right response headers.

    0 讨论(0)
  • 2021-02-06 09:44

    I think you could using blob, something like

    var content=...the content of your request;
    var mypdf = new Blob(content, {type : 'application/pdf'});
    

    and check answer from "panzi" in this other question Using HTML5/Javascript to generate and save a file

    (One character per element in the array seem pretty nice binary. Probably you don't need to transform it. https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Sending_and_Receiving_Binary_Data )

    0 讨论(0)
  • 2021-02-06 09:46

    you can use this instead of above code :

    var url = config.domain + 'file/' + file;
       var parameters = "Authorization=" + user.auth +
                        "&secret-key=" + user.secretkey;
       var reportParameters = url + encodeURIComponent(parameters);
       window.location.assign(reportParameters);
    
    0 讨论(0)
  • 2021-02-06 09:47

    Maybe you could do something like this?

    var a = document.createElement('a');
    a.href = 'data:attachment/pdf,' + encodeURI(data);
    a.target = '_blank';
    a.download = 'filename.pdf';
    a.click();
    

    You'd just have to make sure that data was in the correct format, which IIRC is base64.

    0 讨论(0)
提交回复
热议问题