簡單的問題,我想。
我有一個 html 按鈕:<button type="button" >Download</button>.
單擊時,我必須開始下載與專案位于同一檔案夾中的檔案。我從來沒有使用過任何東西來下載檔案,所以我不知道該怎么做,我也不了解其他問題。有人可以告訴我怎么做,解釋為什么我必須以某種方式做嗎?所有這些在 jquery 中都應該是可取的,但即使是基本的 js 也可以。謝謝。
uj5u.com熱心網友回復:
using<a>完成作業而不涉及javascript
<a href="./file-path/file-name" class="btn btn-success" download>Download</a>
該download屬性將強制下載可視內容。否則,它將被渲染而不是下載。
uj5u.com熱心網友回復:
確實應該為您完成作業。為了更好地理解功能,請考慮以下功能
function DownloadUsingAnchorElement(fileUrl, fileName)
{
const anchor = document.createElement("a");
anchor.href = fileUrl;
anchor.download = fileName;
document.body.appendChild(anchor);
anchor.click();
document.body.removeChild(anchor);
}
檢查以及:https ://www.w3schools.com/tags/att_a_download.asp
uj5u.com熱心網友回復:
document.querySelector('button').addEventListener('click', () => {
const link = document.createElement('a')
link.setAttribute('download', 'filename')
link.setAttribute('href', './path-to-file/filename.ext')
link.click()
})
uj5u.com熱心網友回復:
你也可以這樣做
<button type="button" class="btn btn-success" id="downloadButton">Download</button>
$(document).ready(function () {
$("#downloadButton").click(function (e) {
e.preventDefault();
window.location.href = "your file path";
});
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/487459.html
標籤:javascript jQuery 按钮 下载
