0

I read that axios is good for image uploading to the database and here is what I got so far:

var photo = {
  uri: this.state.userimgSource,
  type: this.state.userimgSourceType,
  name: file, <-- here
  fileName: Image.png
};
var body = new FormData();
body.append('file', photo);
axios({
    method: 'post',
    url: `https://www.example.com/React/user-image-upload.php`,
    data: body,
    config: { headers: {'Content-Type': 'multipart/form-data' }}
})

I am new to Axios and I am not sure why I am getting Can't find variable: file. I tried to put file into a string and react native will not accept that. If file needs to be a variable, what would I define it with?

Laney Williams
  • 573
  • 3
  • 13
  • 34
  • You need to add headers like headers: { 'accept': 'application/json', 'Accept-Language': 'en-US,en;q=0.8', 'Content-Type': `multipart/form-data; boundary=${form._boundary}`, }. If this does not work then you can look here https://stackoverflow.com/questions/39663961/how-do-you-send-images-to-node-js-with-axios – Saurabh Ghewari Jun 30 '18 at 17:16
  • i don't understand why you need to put file into name ? – Selmi Karim Jun 30 '18 at 17:24
  • `name: file` should that the place of `$_FILES['uploaded_file']` in my `upload.php` @SelmiKarim – Laney Williams Jun 30 '18 at 17:26
  • @SaurabhGhewari Also, I did everything you did along with that SO Answer and I am still getting that error. – Laney Williams Jun 30 '18 at 17:45
  • What is the difference between name and fileName? Does it have anything to do with it. – Ashwin Mothilal Jul 01 '18 at 07:58
  • @AshwinMothilal `name` is similar to HTML's `name` when you want to use (for php to my knowledge) server side things and `fileName` is just the name of the image file – Laney Williams Jul 01 '18 at 11:21

0 Answers0