PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, July 31, 2022

[FIXED] How do I upload a file from Axios to Django?

 July 31, 2022     axios, django, file-upload, javascript, reactjs     No comments   

Issue

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


Solution

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



Answered By - Hiroyuki Nuri
Answer Checked By - Pedro (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing