Append object which contain File and String value into FormData - JS

那年仲夏 提交于 2019-12-11 15:19:26

问题


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)

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:


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');


来源:https://stackoverflow.com/questions/51721680/append-object-which-contain-file-and-string-value-into-formdata-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!