第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 AJAX 和 HTML 按鈕下載 PDF 文件(無 jquery)

使用 AJAX 和 HTML 按鈕下載 PDF 文件(無 jquery)

達令說 2021-11-12 15:37:48
我想在按下 HTML 按鈕時下載 PDF 文件。我希望點擊事件在一個 JS 文件中。我需要一些與下面的代碼做同樣的事情,但在一個 JS 文件中:    <a href="url" download>        <button>Download</button>    </a>我嘗試按照此操作,但它沒有創(chuàng)建我的按鈕:var req = new XMLHttpRequest();req.open("GET", "url", true);req.responseType = "blob";req.onreadystatechange = function () {  if (req.readyState === 4 && req.status === 200) {    // test for IE    if (typeof window.navigator.msSaveBlob === 'function') {      window.navigator.msSaveBlob(req.response, "PdfName-" + new Date().getTime() + ".pdf");    } else {      var blob = req.response;      var link = document.createElement('a');      link.href = window.URL.createObjectURL(blob);      link.download = "PdfName-" + new Date().getTime() + ".pdf";      // append the link to the document body      document.body.appendChild(link);      link.click();    }  }};req.send();
查看完整描述

3 回答

?
MM們

TA貢獻1886條經(jīng)驗 獲得超2個贊

如果您已經(jīng)有了 URL,并且只想下載文件,請使用一個不可見的鏈接,然后為用戶單擊它:


function triggerDownload(url, filename) {

  const a = document.createElement('a');

  a.href = url;

  a.download = filename;

  a.style.display = `none`;

  document.body.appendChild(a);

  a.click();

  document.body.removeChild(a);

}

請注意添加到文檔中,這是鏈接算作真正鏈接所必需的。


查看完整回答
反對 回復(fù) 2021-11-12
?
四季花海

TA貢獻1811條經(jīng)驗 獲得超5個贊

我同意邁克,如果您想添加下載時的時間戳,則不需要任何 ajax 來下載它,只需更改下載屬性動態(tài)

<a href="url" onclick="this.download = `PdfName-${+new Date}.pdf`">Download</a>

或者最好使用 content-disposition 附件標頭將其添加到后端


查看完整回答
反對 回復(fù) 2021-11-12
?
叮當(dāng)貓咪

TA貢獻1776條經(jīng)驗 獲得超12個贊

剛剛使它工作,這是我的工作代碼


const blob = new Blob([response], { type: 'application/pdf' });

const link = document.createElement('a');

link.href = window.URL.createObjectURL(blob);

link.download = "filename.pdf";

link.click();


查看完整回答
反對 回復(fù) 2021-11-12
  • 3 回答
  • 0 關(guān)注
  • 284 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號