Is it possible to initiate a download prompt in the browser for recognized MIME types using only JavaScript (client-side approach)?

后端 未结 3 779
孤城傲影
孤城傲影 2020-11-29 10:37

I would like to allow the user to directly download a file with a single click. There is however a problem when it comes to known meme-types like HTML, audio, video, etc. Id

相关标签:
3条回答
  • 2020-11-29 10:57

    There is a new download attribute in HTML5 that you can annote links with. It indicates to the browser that the resource should be downloaded rather than navigated to. Right now, it only works in Chrome, but it is part of the HTML spec and will hopefully be adopted by other browser soon.

    Demo: http://html5-demos.appspot.com/static/a.download.html More info: http://updates.html5rocks.com/2011/08/Downloading-resources-in-HTML5-a-download

    0 讨论(0)
  • 2020-11-29 11:13

    You can use <a href="example" download>. This is HTML5 and it works with Chrome, Firefox and Edge (but not with Internet Explorer, not even modern versions).

    0 讨论(0)
  • 2020-11-29 11:15

    If someone reaches this question now the best solution is

    <a href="example" download target="_blank">
    

    If the browser supports the HTML5 attribute download will start the download of the file, otherwise (in case of Internet Explorer and old browsers) the link will open another tab window with the file to download.

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