我有一个使用flask的Web服务器。 后端中的以下代码支持文件下载:
@images_api_blueprint.route('/api/v1_2/download_file3', methods=['GET'])
def download_file3():
zipFullFileName1 = './foo.zip'
response1 = send_file(zipFullFileName1, as_attachment=True)
return response1
我可以通过两种方式下载文件:
直接从浏览器中-通过在浏览器中输入“ http://localhost/api/v1_2/download_file3”
从我的客户端通过javascript以编程方式-使用以下代码:
。
function downloadFile3() {
let urlToSend = 'http://localhost/download_zip_file2';
let req = new XMLHttpRequest();
req.open("GET", urlToSend, true);
req.responseType = "blob";
req.onload = function (event) {
let blob = req.response;
let fileName = 'zippedFiles.zip';
let link=document.createElement('a');
link.href=window.URL.createObjectURL(blob);
link.download=fileName;
link.click();
};
req.send();
}
在两种情况下都可以下载文件,但我担心Javascript用例。
javascript代码使用blob和createObjectURL(我假设是在将blob下载到文件系统之前在内存中创建的),这引发了以下问题:
谢谢
阿维