Content-Disposition:attachment not triggering download dialog

后端 未结 3 946
猫巷女王i
猫巷女王i 2020-12-29 06:36

I\'ve encountered some unexpected behavior when trying to create a file download functionality on my NodeJS server. I have a REST (express) API that calls for some export da

相关标签:
3条回答
  • 2020-12-29 07:14

    You can try to use a different content-type, so that won't be open as a text file on the browser:

    Content-Type:application/ms-excel; charset=UTF-8
    

    Another alternative could be to use application/octet-stream as mime-type to define it as a downloadable file without a better description.

    Content-Type:application/octet-stream; charset=UTF-8
    
    0 讨论(0)
  • 2020-12-29 07:20

    Headers are not the issue. The issue is that you are querying the download url via an ajax call, which will not invoke the browser download dialog. Your options boil down to the following:

    1. Use a form that is submitted to your download url. Instead of having a visible form a user has to interact with, create a form with JavaScript and submit it programmatically by calling form.submit - Handle file download from ajax post

    2. Point window.location to the download url. You can do this in the current window - download file using an ajax request , or in a new one - res.download() not working in my case

    0 讨论(0)
  • 2020-12-29 07:20

    The question looks similar to this one

    Show 'Save as' dialog box while downloading file from an Iframe through PHP

    The basic idea is the second option that Roman described above, but this one's using an iframe to achieve it.

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