Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 内存中创建的Zip文件已损坏_Javascript_Python_Angularjs_Flask_Zipfile - Fatal编程技术网

Javascript 内存中创建的Zip文件已损坏

Javascript 内存中创建的Zip文件已损坏,javascript,python,angularjs,flask,zipfile,Javascript,Python,Angularjs,Flask,Zipfile,我正在尝试向JS前端提供一个zip文件(由Flask在内存中创建)。下载的文件已损坏,我不明白我做错了什么 @app.route('/route') def test_zip(): zf = BytesIO() with zipfile.ZipFile(zf, 'w') as archive: archive.writestr("test.csv", "test, 2") zf.seek(0) return send_file( z

我正在尝试向JS前端提供一个zip文件(由Flask在内存中创建)。下载的文件已损坏,我不明白我做错了什么

@app.route('/route')
def test_zip():
    zf = BytesIO()
    with zipfile.ZipFile(zf, 'w') as archive:
        archive.writestr("test.csv", "test, 2")
    zf.seek(0)
    return send_file(
        zf,
        attachment_filename='test.zip',
        mimetype='application/x-zip-compressed',
        as_attachment=True
    )
$http({
url:settings.BACKEND_url+“/route”
}).成功(功能(数据){
var anchor=角度元素(“”);
var blob=new blob([data],{'type':'application/zip'});
anchor.attr({
href:window.URL.createObjectURL(blob),
目标:“\u blank”,
下载:“test.zip”
})[0]。单击();
}) 
我(在Mac OS X上)得到的错误如下:

26 extra bytes at beginning or within zipfile
  (attempting to process anyway)
error [test.zip]:  start of central directory not found;
  zipfile corrupt.
  (please check that you have transferred or created the zipfile in the
  appropriate BINARY mode and that you have compiled UnZip properly)

正如评论中指出的,Python部分工作得很好。修复JS部件的解决方案是添加

responseType: 'arraybuffer'
在http请求的参数中


参考:

您是否尝试过使用十六进制编辑器来比较下载的文件和生成的文件?@RandomDavis我不确定您的意思,因为文件是在内存中生成的。请在发送前添加代码将
zf
保存到本地文件,然后进行比较。@JohnGordon抱歉,我忘了提到,如果我先将文件存储在文件系统中,那么一切都会正常工作。我想了解的是,当我试图将所有内容都保存在内存中时,到底出了什么问题。python部分对我来说很有用。成功函数中的数据是什么?