作者:mobiledu2502901317 | 来源:互联网 | 2023-06-02 17:03
GET
JS中的请求与访问浏览器中的URL不同。您需要通过指定URL直接在客户端上调用下载,例如:
download() {
// fake server request, getting the file url as response
setTimeout(() => {
const respOnse= {
file: 'http://releases.ubuntu.com/12.04.5/ubuntu-12.04.5-alternate-amd64.iso',
};
// server sent the url to the file!
// now, let's download:
window.open(response.file);
// you could also do:
// window.location.href = response.file;
}, 100);
}
这是JSBin上的工作示例。
,如果要下载浏览器可以显示的文件(例如JSON,图像,视频),它们将显示在新选项卡中。如果要直接下载这些类型的文件,则需要使用一些解决方法,例如使用blob
。