使用Vue axios和Django Python后端上传文件



我有VueJs作为我的前端,我有上传组件,并使用formdata创建请求和axios对用Django Python编写的后端进行rest api调用。

My Front上传方法看起来像:

uploadFiles() {
if (!this.currentFile) {
this.message = "Please select a file!";
return
} else {
let formData = new FormData();
formData.append("file", this.currentFile);
const instance = axios.create({
baseURL: "<URL>",
withCredentials: false,
});
instance
.post("/test/", formData, {
headers: {
"content-type": "multipart/form-data",
},
})
.then((response) => {
console.log("Success!")
console.log({ response })
})
.catch((error) => {
console.log({ error })
})
}
},

现在支持的微服务是DjangoPython,我想在这里使用该文件。然而,我在这里没有得到文件。

代码如下

@api_view(['POST'])
def attachment(request):
myFile = request.FILES
myfile2 = request.data.initial_data

我尝试了以上两种方法从请求对象中提取文件,但都没有成功,但如果我通过邮递员发送请求,它就可以了。

如果它使用postman工作,您可能需要比较postman和前端发送的邮件头。使用请求。META在django看看有什么不同。

最新更新