簡體   English   中英

HTML通過鏈接下載文件

[英]HTML download the file by link

通過鏈接下載文件不起作用。 單擊后,將轉到瀏覽器的下一個選項卡並打開文件。 Chrome 65版本

<a download href="images/download.png">download</a>

嘗試更改路徑,如果路徑不正確,它將打開一個新選項卡

 <a href="/images/download.png" download> Download </a> 

進一步參考https://www.w3schools.com/tags/att_a_download.asp

使用application/octet-stream

<a href="/images/download.png" download type="application/octet-stream "> Download </a>

就是這樣!!!
有關更多格式: https : //www.stubbornjava.com/posts/what-is-a-content-type

$('a').click(function(e) {
    e.preventDefault();  //stop the browser from following
    window.location.href = 'uploads/file.doc';
});

<a href="withoutscript.html">Download now!</a>

試試這個有jQuery或無jQuery代碼都可以

也嘗試這個,將對您有幫助

 $('.download-file').on('click', function(e) { // For IE if(window.navigator.msSaveOrOpenBlob) { var blobObject = new Blob(['<p>Hello world!</p>'], {type : 'text/html'}); window.navigator.msSaveOrOpenBlob(blobObject, 'somefile.html'); e.preventDefault(); } // For browsers that support the `download` attribute else { // You could also create the url with `window.URL.createObjectURL` // var blobObject = new Blob(['<p>Hello world!</p>'], {type : 'text/html'}); // this.href = window.URL.createObjectURL(blobObject) this.href = 'data:text/html;charset=utf-8,' + encodeURIComponent('<p>Hello world!</p>'); } /* * / // Doesn't work, idk. It was an attempt for an IE solution // Suggested here: http://stackoverflow.com/a/25179390/796832 // and here: http://stackoverflow.com/a/25179390/796832 var bufferIframe = document.createElement('iframe'); bufferIframe.setAttribute('sandbox', 'allow-same-origin'); bufferIframe.setAttribute('srcdoc', '<p>Hello world!</p>'); document.body.appendChild(bufferIframe); bufferIframe.contentWindow.document.open('text/plain', 'replace'); // Keep jsFiddle from complaining about the `write` using bufferIframe.contentWindow.document['write']('<p>write Hello world!</p>'); bufferIframe.contentWindow.document.close(); //console.dir(bufferIframe.contentDocument); bufferIframe.contentWindow.document.execCommand('SaveAs', true, 'request.bin'); /* */ }); 
 <p>Download a file demo</p> <a href="http://i.stack.imgur.com/kaxff.png" download="savename.png">Download `download` attribute</a> <br> <a href="#0" class="download-file" download="some-file.html">Download via JS</a> <br> <hr> Note: <ul> <li>You could always just add the `Content-Disposition: attachment; filename="somefile.html"`</li> <li>You can even add the header in .htaccess: `Header set Content-Disposition "attachment"`</li> </ul> 

供參考https://jsfiddle.net/MadLittleMods/dywbo5vx/

如果鏈接路徑不正確,下載鏈接不起作用,請檢查圖像路徑是否正確? 然后檢查下載鏈接是否正常?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM