【发布时间】:2021-01-22 13:32:28
【问题描述】:
我正在尝试将文件上传到目录中,我在 Python 中使用 vue.js 和 fastapi。
但是每次我得到一个错误 422 无法处理的函数。
我尝试这样使用官方文档:
<!-- Vue Template -->
<input
style="display: none"
type="file"
name="files"
ref="fileAdd"
v-on:change="handleFilesChange()"
/>
// Vue Script
handleFilesChange() {
var uploadedFiles = this.$refs.fileAdd.files;
if (uploadedFiles.length > 0) {
for (var i = 0; i < uploadedFiles.length; i++) {
this.files.push(uploadedFiles[i]);
var formData = new FormData();
formData.append("files", this.$refs.fileAdd.files[0]);
axios
.post(url + "/uploadfile", formData, {
headers: {
"Content-Type": "multipart/form-data",
},
onUploadProgress: (uploadStatus) => {
this.uploadProgress = Math.round(
(uploadStatus.loaded / uploadStatus.total) * 100
);
this.isUplading = true;
},
})
.then((response) => {do something}
}
}
}
# FastAPI endpoint
@app.post('/uploadfile')
async def upload_file(sid: str, files: List[UploadFile]=File(...)):
print(files)
if(files == []):
print("No Files")
else:
print(files[0].filename)
#...do something
我得到错误 422 无法处理。
知道如何解决这个问题吗?
【问题讨论】:
标签: python python-3.x vue.js vuejs2 fastapi