将包含文件和字符串值的对象追加到FormData中-JS [英] Append object which contain File and String value into FormData - JS

查看:707
本文介绍了将包含文件和字符串值的对象追加到FormData中-JS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种将包含File和String值的对象附加到FormData中并将其发送到服务器(Django Rest FrameWork的MultiPartParser)的解决方案.

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(文件)

Console.log(file)

现在我的代码是:

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

当我用console.log记录表单数据的值时,它返回{"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.

我试图删除JSON.stringify:

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

当我用console.log记录表单数据的值时,它返回[object, object].

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

我希望结果是

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

推荐答案

您不能将文件对象和键值附加到FormData中.尝试这样的替代解决方案

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

i.e)我将在文件名中添加order_no,在python中,您可以使用字符串split函数获取order_no

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

这篇关于将包含文件和字符串值的对象追加到FormData中-JS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆