我正在尝试使用React Native将图像保存到php应用程序(由laravel 6.0制作)中。这是我的反应本机图像选择器
var ImagePicker = NativeModules.ImageCropPicker;
这是我的图像保存功能
addImages = async () => {
const { image,images } = this.state
const access_token = await AsyncStorage.getItem('access_token')
try {
let data = new FormData();
images.map((image,i) => {
data.append('id',id);
data.append('uri',image.uri);
data.append('type',image.mime);
data.append('name','test.jpg');
});
fetch(apiConfig.apiUrl + '/api/save-image',{
method: 'POST',headers: {
'Content-Type' : 'multipart/form-data','Authorization': 'Bearer ' + access_token,},body:data
})
.then(function (response) {
return response.json();
})
.then(function (data) {
try {
console.log(data);
} catch (error) {
console.log(error);
}
}.bind(this))
.catch((error) => {
console.log(error)
});
}catch (error) {
console.log(error)
}
}
这是我的php代码
public function saveImage(Request $request)
{
header( "access-Control-Allow-Methods' => 'POST,GET,OPTIONS,PUT,DELETE");
header("access-control-allow-origin: *");
try {
$file= array('file'=>$request->uri);
Storage::disk('public')->put($request->imgName,File::get($file->file));
return response()->json(['true'=>'Successfully Created'],200);
} catch (\Exception $e) {
Log::info('vehicle image: ',[$e->getMessage()]);
return response()->json(['error'=>$e],200);
}
}
当我尝试保存时,我得到SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data
。
当我退回$request->uri
时,我得到的是这样的file:///data/user/0/com.carup/cache/react-native-image-crop-picker/IMG_20191103_161929.jpg
我该如何解决?
我该如何解决?