[英]How to upload an image on django server with react native?
I'm using react native on frontend and django in backend.我在前端使用本机反应,在后端使用 django。 I want to submit a form and that form contains images in it.
我想提交一个表单,该表单中包含图像。 Now the problem is.
现在问题来了。 I'm unable to upload images with following code.
我无法使用以下代码上传图片。 currently following json is going in backend.
目前跟随 json 正在后端。
{
"details":{
"name":"My first ",
"description":"this is Gina be cool",
"display":"file:///data/user/0/com.merchant_app/cache/rn_image_picker_lib_temp_935cb7c5-72fd-4c7a-ab45-7b62906909f7.jpg",
"background":"file:///data/user/0/com.merchant_app/cache/rn_image_picker_lib_temp_9449c3c0-32c6-4122-8a92-1a9b5afa352f.jpg"
},
"hours":[
{
"partition":"P1",
"timings":[
{
"day":"Monday",
"full_day":false,
"close_day":false,
"start_time":"2022-09-16T01:00:00.149Z",
"close_time":"2022-09-16T01:00:00.149Z"
}
]
}
]
}
backend response:后端响应:
{'display': ['The submitted data was not a file. Check the encoding type on the form.'], 'background': ['The submitted data was not a file. Check the encoding type on the form.']}
my code.我的代码。
const {Name,bio,displayFilePath,backgroundFilePath} = this.props.route.params.stepOneData
const background = backgroundFilePath
const display = displayFilePath
console.log("==> ",backgroundFilePath.assets[0].fileName)
let data = {
details = {
name:Name,
description:bio,
display:display,
background:background
},
hours:[
{
"partition":"P1",
"timings":[
{
"day":"Monday",
"full_day":false,
"close_day":false,
"start_time":"2022-09-16T01:00:00.149Z",
"close_time":"2022-09-16T01:00:00.149Z"
}
]
}
]
}
try {
await addNewGroundAPI(formdata)
alert("Post created")
}
catch (e) {
console.log("Error", e)
// alert("Error")
}
If you know to fix it.如果你知道修复它。 Please contribute in solving this thank You.
请为解决这个问题做出贡献,谢谢。
Use the following format to pass image object to server使用以下格式将图像 object 传递给服务器
{
name: //image name,
type: //image mime can be found at backgroundFilePath.assets[0].mime,
uri: //image path can be found at backgroundFilePath.assets[0].path,
size: //image size can be found at backgroundFilePath.assets[0].size,
}
Also, Please make sure you are passing Form data object (new FormData()) to your server.另外,请确保您将表单数据 object (new FormData()) 传递到您的服务器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.