https://www.zhangxinxu.com/wordpress/2017/07/js-text-string-download-as-html-json-file/ 侵刪 1.H5 download屬性 downFile(下載地址, 保存名稱); 2.iframe方式 3.form方式 ...
https://www.zhangxinxu.com/wordpress/2017/07/js-text-string-download-as-html-json-file/ 侵刪
1.H5 download屬性
function downFile(content, filename) { // 創建隱藏的可下載鏈接 var eleLink = document.createElement('a'); eleLink.download = filename; eleLink.style.display = 'none'; // 字元內容轉變成blob地址 var blob = new Blob([content]); eleLink.href = URL.createObjectURL(blob); // 觸發點擊 document.body.appendChild(eleLink); eleLink.click(); // 然後移除 document.body.removeChild(eleLink); };
downFile(下載地址, 保存名稱);
2.iframe方式
// if (typeof(download.iframe) == 'undefined') { // var iframe = document.createElement('iframe'); // download.iframe = iframe; // document.body.appendChild(download.iframe); // }; // download.iframe.src = newdownloadUrl; // download.iframe.style.display = "none";
3.form方式
// var $eleForm = $("<form method='get'></form>"); // $eleForm.attr("action", "https://codeload.github.com/douban/douban-client/legacy.zip/master"); // $eleForm.attr("action", url); // $(document.body).append($eleForm); // $eleForm.submit();