我要从Jquery切换到react dropzone& Axios,我想上传一个文件到我的Django服务器,我没有问题在服务器上发布图像的blob url,但是我想在request.FILES
下得到它,但是我得到了一个空的查询集。
request.FILES : <MultiValueDict: {}> <!--- empty
request.POST : <QueryDict: {}> <!--- able to get a blob url
下面是我的axios配置:
const temporaryURL = URL.createObjectURL(step3.drop[0]);
var fd = new FormData();
fd.append('image', temporaryURL);
axios({
method: 'post',
url: SITE_DOMAIN_NAME + '/business-card/collect/',
data: fd,
headers: {
"X-CSRFToken": CSRF_TOKEN,
"content-type": "application/x-www-form-urlencoded"
}
}).then(function (response) {
console.log(response)
URL.revokeObjectURL(temporaryURL);
}).catch(function (error) {
console.log(error)
});
我收到了一个classBasedView上的文件在邮局的请求。
我怎么上传文件?我哪里错了?
编辑
我也尝试过“应用程序/表单数据”,但没有解决这个问题。
发布于 2018-03-23 13:51:43
问题来自于content-type
,因为它使用的是"application/form-data"
而不是"multipart/form-data"
。
发布于 2022-05-18 14:49:16
我的回答是,有人通过在谷歌上搜索来这里:
let formData = new FormData();
formData.append('myFile', file);
formData.append('otherParam', 'myValue');
axios({
method: 'post',
url: 'myUrl',
data: formData,
headers: {
'content-type': 'multipart/form-data'
}
}).then(function (response) {
// on success
}).catch(function (error) {
// on error
});
https://stackoverflow.com/questions/49434403
复制相似问题