【发布时间】:2020-07-31 04:30:40
【问题描述】:
我需要帮助,我正在使用 react native 和 api laravel 上传图片但不上传?
这是我的反应原生代码
var dataphoto = this.state.photoData; // this from ImagePicker
RNFetchBlob.fetch('POST', 'my_url/api/storimage', {
Authorization : "Bearer access-token",
otherHeader : "foo",
'Content-Type' : 'multipart/form-data',
}, [
{ name : 'image', filename : 'image.jpg',type:'image/jpg', data: dataphoto},
]).then((resp) => {
console.log('response: ' + JSON.stringify(resp));
}).catch((err) => {
console.log('err: ' + JSON.stringify(err));
})
这是我的 Laravel 代码
// Route::post('storimage','apiController@storimage'); **api route**
public function storimage(Request $req){ // the function
if($req->hasFile('image'))
{
$image = $req->file('image');
$name = time().'.' . $image->getClientOriginalName();
\Image::make($req->file('image'))->save(public_path('images/').$name);
return response()->json('Successfully yes');
}
else{
return response()->json('not');
}
}
为什么我的代码不起作用!
【问题讨论】:
-
你找到解决办法了吗,我也遇到了同样的问题
标签: laravel api react-native file-upload react-native-fetch-blob