Prompt file download

前端 未结 5 1865
一生所求
一生所求 2020-12-11 14:39

I have a link on my page on click of which I am trying to generate a PDF document and then show the \"Open - Save\" prompt on the browser.

My HTML (reactjs component

5条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-11 15:22

    Your response from the server looks good. The missing part is that you are not handling this response from the client side in the correct way.

    Lets assume that your resource url object looks like below in js. (i.e. you already know the resource url, if you don't know it yet, then you will need a separate call to the server to know the download url)

    response.downloadUrl = app/media/fileId/document.pdf
    

    All you need to do is set,

    window.location = item.downloadUrl;
    

    This will cause the browser to request a resource from the server, the response from the server must include Content-Disposition:attachment;. It will cause the browser to show the download dialog.

    P.S. I have recently worked on a similar functionality. If you have questions please ask.

    When you want to force the browser to show the download prompt for some files (or resources), you must include Content-Disposition:attachment; in the response header (which you already did).

提交回复
热议问题