我需要通过按下按钮开始下载,我尝试使用以下代码,但它不会开始下载,只需在浏览器中打开一个新的选项卡并显示内容。我做错了?
dataurl与以下内容完全类似:http://localhost:8000/storage/shipment/b61ffc10-6798-4b94-ba91-daec6efbbdfdShipment2020-10-05.txt
download: function(name, dataurl) {
const link = document.createElement("a");
link.setAttribute("download", name+'.txt');
link.setAttribute("target", "_blank")
link.href = dataurl;
document.body.appendChild(link);
link.click();
}
我还尝试将url转换为Blob
const url = window.URL.createObjectURL(new Blob([data]));
const link = document.createElement("a");
link.href = url;
link.setAttribute("download", filename);
document.body.appendChild(link);
link.click();
},
这样,它触发了下载,但内容是url,而不是文件本身。
知道吗?谢谢
如果你想从服务器下载内容,试试这个,它很简单。。。
<a href="http://localhost:8000/storage/shipment/b61ffc10-6798-4b94-ba91-daec6efbbdfdShipment2020-10-05.txt" download="filename-here"> Click here to Download </a>
在<a>
标签中添加了Download
,当点击此链接时,文件将下载为filename-here.txt