Append object which contain File and String value

2019-08-27 22:02发布

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}

1条回答
乱世女痞
2楼-- · 2019-08-27 22:23

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');
查看更多
登录 后发表回答