hug*_*gie 5 python flask vue.js
我的 Flask 是一个 REST 服务器。
在我的烧瓶路线上,这就是我所拥有的:
@app.route('/v1/download', methods=['POST'])
def download_tissue():
f = open('path_to_zip_file', 'rb') // or r for text file
return f.read()
Run Code Online (Sandbox Code Playgroud)
(以前我使用的是 Flask 的send_file()。但我不确定 send_file 做了什么,我无法仅阅读它,并且我正在尝试简化案例以找出问题。)
在客户端上,我有一个发出异步请求的 Vue 应用程序:
axios.post('download')
.then((res) => {
let data = res.data;
const blob = new Blob([data], { type: 'application/zip' })
let link = document.createElement('a')
link.href = window.URL.createObjectURL(blob)
link.download = 'test.zip'
link.click()
})
.catch(error => {
console.error(error);
});
Run Code Online (Sandbox Code Playgroud)
问题是我无法在 Mac 上解压它。无法将 test.zip 展开到下载中。(错误 1 - 不允许操作。)文件大小也是错误的。它比原来的 12 MB 增加了近 22 MB。
如果我打开纯文本文件,它可以工作,但不能打开 zip 文件。
好吧,我想通了。我实际上需要像这样配置 axios 响应类型:
axios({
url: url
method: 'POST',
responseType: 'blob', // important
})
.then((res) => {
})
Run Code Online (Sandbox Code Playgroud)
实际上,我更喜欢使用默认 URL 设置,但以下内容对我不起作用。
axios.post('download', {
responseType: 'blob',
})
.then((res) => {
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3321 次 |
| 最近记录: |