最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do I upload a file from Axios to Django? - Stack Overflow

programmeradmin0浏览0评论

I'm switching from Jquery AJAX to react-dropzone & Axios, I'd like to upload a file to my Django server, I have no issue posting a blob url of the image on the server but I want to get it under request.FILES but I am getting an empty queryset.

request.FILES : <MultiValueDict: {}>  <!--- empty
request.POST  : <QueryDict: {}>       <!--- able to get a blob url

Here's what my axios configuration looks like :

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)
});

I am receiving the file on a classBasedView on POST request.

How can I upload the file? Where am I wrong?

Edit

I also tried "application/form-data", doesn't solve the problem

I'm switching from Jquery AJAX to react-dropzone & Axios, I'd like to upload a file to my Django server, I have no issue posting a blob url of the image on the server but I want to get it under request.FILES but I am getting an empty queryset.

request.FILES : <MultiValueDict: {}>  <!--- empty
request.POST  : <QueryDict: {}>       <!--- able to get a blob url

Here's what my axios configuration looks like :

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)
});

I am receiving the file on a classBasedView on POST request.

How can I upload the file? Where am I wrong?

Edit

I also tried "application/form-data", doesn't solve the problem

Share Improve this question edited Mar 22, 2018 at 17:08 Horai Nuri asked Mar 22, 2018 at 17:00 Horai NuriHorai Nuri 5,58817 gold badges84 silver badges136 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

the problem came from the content-type as it was using "application/form-data" instead of "multipart/form-data".

I am answering in case, someone es here by searching on google:

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 
});
发布评论

评论列表(0)

  1. 暂无评论