怎么用js通过文件的url下载文件到本地

目前拿到的是文件的url,不是下载地址,只是文件的存放地址。前端怎样用js通过这个url实现文件的下载呢?


大话西游666
浏览 8690回答 1
1回答

收到一只叮咚

<!DOCTYPE html><html><head>&nbsp; &nbsp; <meta charset="utf-8">&nbsp; &nbsp; <title></title></head><body>&nbsp; &nbsp; <a href="/download/papers/abc.doc">点击链接下载</a>&nbsp; &nbsp; <button onclick="download1()">点击按钮下载</button>&nbsp; &nbsp; &nbsp; <button onclick="download2">点击按钮下载</button>&nbsp; &nbsp; <script>&nbsp; &nbsp; &nbsp; &nbsp; // 会打开一个空白页下载,然后空白页消失,用户体验不好&nbsp; &nbsp; &nbsp; &nbsp; function download1() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; window.open('/download/papers/1');&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; // 直接下载,用户体验好&nbsp; &nbsp; &nbsp; &nbsp; function download2() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var $form = $('<form method="GET"></form>');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $form.attr('action', '/download/papers/1');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $form.appendTo($('body'));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $form.submit();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; </script></body></html>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript