我有一个Vue.js应用,试图从Flask服务器请求一个zip文件。 但是,当我从服务器收到有效负载并尝试将其打开时,我正在使用的包( JSZip )告诉我zip文件已损坏。 如果我通过浏览器请求url,则zip文件下载不会有问题。 我认为这可能是生成zip文件的方式,但是我不确定。 为什么文件会在客户端损坏?
客户端Javascript代码:
const jszip = require('./jszip.min.js');
...more code...
this.filesystem.REST.get('http://localhost:3000').then(function(result){
var zip = new jszip();
zip.loadAsync(result.data).then(function(contents) {
// Execution does not reach this point
// Fails with corruption error before the then() call
})
})
服务器Python代码:
@app.route('/')
def home():
playerFp = os.path.join(seriesMap[seriesid], playerid)
fileList = os.listdir(playerFp)
bytesIo = io.BytesIO()
zf = zipfile.ZipFile(bytesIo, mode="w")
for file in fileList:
if '.jpg' in file or '.xml' in file:
absFp =os.path.join(playerFp, file)
if '.xml' in file:
stats = getJsonFormat(absFp)
jsonfile = file.replace('.xml', '.json')
zf.writestr(jsonfile, stats)
else:
zf.write(absFp, os.path.relpath(absFp, playerFp))
zf.close()
bytesIo.seek(0)
return send_file(bytesIo, attachment_filename=playerid+'.zip', as_attachment=False)
这是我在控制台中遇到的错误: