How do I force download with HTML/JavaScript?

后端 未结 3 771
长发绾君心
长发绾君心 2020-11-30 10:41

I have a link and, if a user clicks it, I need 2 things to happen:

  • A proper HTTP response is sent to the user (especially with Content-Type: video/mp4
3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-30 11:27

    No, this is not possible (at least for values of JavaScript limited to client-side JavaScript).

    If you want to override the default behavior of how a browser handles an HTTP resource, you need to do it with HTTP headers. If you want to do it correctly, use the content-disposition header (with an attachment value).

    You can set this header using server-side JavaScript or (pretty much) any other server side environment.

提交回复
热议问题