0

I'm finding a solution to append object which contain File and String value into FormData and send it to server (MultiPartParser of Django Rest FrameWork).

Console.log(file) enter image description here

Now my code is:

Fd.append('file_uploads', JSON.stringify({ 'file': file, 'order_num': 1 }) )

When I console.log this value of form data, it returns {"file":{},"order_num":1}. You can see file value is empty.

I tried to remove JSON.stringify:

Fd.append('file_uploads', { 'file': file, 'order_num': 1 } )

When I console.log this value of form data, it returns [object, object].

I want the results is

{"file":<file_object>,"order_num":1}

7
  • 1
    What you get in console.log(files) Commented Aug 7, 2018 at 9:09
  • @RajaSimon edited with photo Above, bro Commented Aug 7, 2018 at 10:02
  • 1
    Can you try this Fd.append('file_uploads', file, 'your_filename_here'); Commented Aug 7, 2018 at 10:07
  • @RajaSimon It's ok. But I want to send to server JSON string {"file":<file_object>,"order_num":1} Commented Aug 7, 2018 at 10:10
  • Okay why not append another one like this Fd.append('json_load', {..}) in another line Commented Aug 7, 2018 at 10:15

1 Answer 1

1

You can not append file object and key value with FormData. Try alternative solution like this

i.e) I will add order_no with file name and in python you can use string split function to get the order_no

Fd.append('file_uploads', file, 'your_filename_here_and_order_no');
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.