使用PHP服务器响应本机图像上载
无法将图像从react本机代码上载到PHP服务器,但是使用postman上载图像没有任何问题,我想问题与服务器或后端无关,但我将提供前端和后端代码 我尝试了不同的库,如react native document picker和react native image crop picker,但没有希望,所以请告诉我问题到底出在哪里 PHP代码使用PHP服务器响应本机图像上载,php,image,react-native,upload,Php,Image,React Native,Upload,无法将图像从react本机代码上载到PHP服务器,但是使用postman上载图像没有任何问题,我想问题与服务器或后端无关,但我将提供前端和后端代码 我尝试了不同的库,如react native document picker和react native image crop picker,但没有希望,所以请告诉我问题到底出在哪里 PHP代码 public function createApiImage(Request $request) { $attachment = []
public function createApiImage(Request $request)
{
$attachment = [];
if ($request->attachments != null) {
$attachment = $request->attachments ;
$photo_name = 'ads';
$imgPath = $attachment->store("{$photo_name}", 'public');
$attachment1 =[
'type' => $attachment->getMimeType(),
'path' => $attachment->store("{$photo_name}", 'public'),
'name' => $attachment->getClientOriginalName(),
'created_at' => \Carbon\Carbon::now()
];
$imgPathUrl = 'http://dejara.net/storage/app/public/'.$imgPath;
$Mediadate = ['name' => "$imgPathUrl",'linked_id' => 0];
$media = Media::create($Mediadate);
}
return response()->json([
'success' => 'true',
'info' => [
'Media' => $media,
]
]
, 200
);
}
反应本机代码
import React, { Component } from 'react';
import ImagePicker from 'react-native-image-picker'
import { Text, View, TouchableOpacity, BackHandler, Image, ScrollView, Platform, ActivityIndicator, StatusBar, Dimensions } from 'react-native'
import { RFValue } from 'react-native-responsive-fontsize';
import FontAwesomeIcon from 'react-native-vector-icons/FontAwesome'
class PlaceAd extends Component {
state = {
pickedImages: []
}
pickImage = async () => {
ImagePicker.showImagePicker({ title: "choose your image" }, res => {
if (res.didCancel)
console.log('User cancelled')
else if (res.error)
console.log(res.error)
else {
// console.log(res)
let temp = this.state.pickedImages
temp.push({ uri: res.uri, name: res.fileName, type: res.type, path: res.path })
this.setState({ pickedImages: temp })
}
})
}
createAdv = () => {
var data = new FormData();
this.state.pickedImages.map((image, i) => {
data.append('my_photo', {
uri: image.uri,
path: image.uri,
name: image.name,
type: image.type,
})
})
fetch('http://dejara.net/public/api/createAdsImage', {
headers: {
'Accept': 'application/json',
'Content-Type': 'multipart/form-data'
},
method: 'POST',
body: data
})
.then(response => response.json())
.then(res => {
console.log(res)
})
.catch(err => {
console.log(err)
})
}
render() {
return (
<View style={{width:'100%', alignItems:'center'}}>
<TouchableOpacity style={{ marginBottom: RFValue(20), marginTop: RFValue(10), width: RFValue(150), height: RFValue(150), justifyContent: 'center', alignItems: 'center', borderWidth: 1 }} onPress={this.pickImage}>
<FontAwesomeIcon name="photo" size={RFValue(20)} color="#000" />
<Text style={{ color: "#000" }}>choose photo</Text>
</TouchableOpacity>
<TouchableOpacity style={[{ width: '60%', height: RFValue(50), marginBottom: RFValue(15) }]} onPress={this.createAdv} >
<Text>Publish</Text>
</TouchableOpacity>
</View>
)
}
}
export default PlaceAd
import React,{Component}来自'React';
从“反应本机图像选择器”导入图像选择器
从“react native”导入{文本、视图、TouchableOpacity、BackHandler、图像、ScrollView、平台、ActivityIndicator、状态栏、维度}
从'react native responsive fontsize'导入{RFValue};
从“反应本机矢量图标/FontAwesome”导入FontAwesomeIcon
类PlaceAd扩展组件{
状态={
PickeImage:[]
}
pickImage=async()=>{
showImagePicker({title:“选择您的图像”},res=>{
如果(取消)
console.log('用户已取消')
else if(res.error)
console.log(res.error)
否则{
//console.log(res)
让temp=this.state.pickedImages
临时推送({uri:res.uri,name:res.fileName,type:res.type,path:res.path})
this.setState({pickedImages:temp})
}
})
}
createAdv=()=>{
var data=new FormData();
this.state.pickedImages.map((图像,i)=>{
data.append('我的照片'{
uri:image.uri,
路径:image.uri,
name:image.name,
type:image.type,
})
})
取('http://dejara.net/public/api/createAdsImage', {
标题:{
“接受”:“应用程序/json”,
“内容类型”:“多部分/表单数据”
},
方法:“POST”,
正文:数据
})
.then(response=>response.json())
。然后(res=>{
console.log(res)
})
.catch(错误=>{
console.log(错误)
})
}
render(){
返回(
选择照片
发表
)
}
}
导出默认位置AD
我想我发现了问题,请一步一步解决,它可能会解决:
1- npm install form-data
2- import formData from 'from-data';
3- change var to const ===> const data = new FormData();
最重要的是安装表单数据包来解决此问题
在此之前,请使用postman测试您的后端,以确保其正常工作,问题只与fronend有关,然后按照这些步骤进行操作,我希望您能够解决它。我知道了,服务器只是等待附件
if ($request->attachments != null)
而我却发了我的照片
data.append('my_photo',
只是把我的照片改成附件就解决了我的问题