将图像文件从react-native上传到php服务器 [英] Upload image file to php server from react-native

查看:217
本文介绍了将图像文件从react-native上传到php服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将反应原生的图像文件上传到我的php服务器。我的问题是如何将文件对象发布到php。

I am attempting to upload an image file from react native to my php server. My problem is how to post a file object to php.

我的javascript代码:

My javascript code:

 storePicture(){
    const file = {
     uri: this.state.selected[0].uri,
     name: this.state.selected[0].filename,
     type: 'image/jpg'
    }


   fetch('http://localhost:8888/s3/index.php', {
     method: 'POST',
     body: JSON.stringify({
       file: file
     })
   })

   .then((response) => response.json())
   .then((responseJson) => {
     console.log(responseJson.status)

   })
}

我的PHP代码:

$data_back = json_decode(file_get_contents('php://input'));

$file = $data_back->{"file"};


推荐答案

你不能像这样进行字符串化。尝试创建一个FormData对象,将文件附加到它并将FormData添加到body。

You cannot stringify like that. Try to create a FormData object, append the file to it and add the FormData to body.

let formData = new FormData();
formData.append("image", this.state.fileInputElement.files[0]);

fetch('http://localhost:8888/s3/index.php', {
 method: 'POST',
 body: formData
})

这篇关于将图像文件从react-native上传到php服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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