【问题标题】:fetch-blob-not post image not wroking with Laravel api - react nativefetch-blob-not post 图像不适用于 Laravel api - 反应原生
【发布时间】: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


【解决方案1】:

您发送数据图像,因此在服务器中接收 base64,您可以:

$base64Image = $req->get('image');
$data = base64_decode($base64Image);
file_put_contents($imagePath, $data);

$imagePath 是你保存的地方

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-04-11
    • 2019-02-28
    • 2017-11-16
    • 1970-01-01
    • 2021-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多