0

I have a React Native app, there is a screen to upload a logo (an image). When the logo gets uploaded, Django should receive the image, save into an user row and create the file into local assets folder (as usual). The problem is, i don't even know if i am sending the data from React Native in a correct way, but Django REST API is receiving the image as an Python dict. I'm following the instructions from many other people, but still not working, here's how i load the logo into a React Native FormData instance:

const fd = new FormData();
fd.append('logo', {
  name: logo.fileName,
  uri: logo.uri,
  type: logo.type,
});

The logo variable is an Asset instance, from react-native-image-picker library.

And here, how Django receive the data, with pdb (Python debugger):

enter image description here

For a better look:

{'_parts': [
  ['logo', {
    'name': 'logo.png',
    'uri': 'file:///data/user/0/com.micandidato/cache/rn_image_picker_lib_temp_9b3907e0-3f13-4ad5-9ca9-13f491473440.png',
    'type': 'image/png'
  }]
]}

I'm pretty sure Django is not going to save this object as an image. The request method from React Native is PATCH (if relevant). Thanks.

gianlop3z
  • 107
  • 2
  • 9

1 Answers1

0

To append data (i.e. image) in FormData instance we have to pass the arguments like:

#at FrontEnd

const fd = new FormData();
fd.append('image',imageFileOrBlob,FileName*);
//rest of the code

FileName is optional argument.

The imageFileOrBlob in your case should be passed like yourInputField.target.files[0]

Reference

#at Django backEnd

def myView(request):
  logo = request.FILES.get('image')
  #rest of the code
itsmehemant7
  • 339
  • 1
  • 8