在本机中上传多张图片

时间:2020-09-13 09:19:30

标签: react-native

我想将一些图像上传到服务器并保存到我的数据库的路径 我用2个主包装 1.react-native-fetch-blob 2.react-native-image-crop-picker

但是最后我无法在api服务器上获取文件 有人可以帮忙吗?

选择器功能:

openImagePicker() {
ImagePicker.openPicker({
  multiple: true, // To support multiple image selection
  quality: 1.0,
  maxWidth: 500,
  maxHeight: 500,
}).then((image) => {
  for (var i = 0; i < image.length; i++) {
    JSON.stringify(this.state.images.push(image[i].path));
  }
});

}

使用fetch blob上传功能是:

async upload() {
console.log(this.state.images);
await RNFetchBlob.fetch(
  'POST',
  'http://192.168.1.35:8000/uploadprofile2/',
  {
    Authorization: 'Bearer access-token',
    otherHeader: 'foo',
    'Content-Type': 'multipart/form-data',
  },
  [
    this.state.images.map((item, key) => (
      {name: 'photo[]', filename:'avatar.png'  , data: RNFetchBlob.wrap(item)}
      )),
  ],
).then((resp) => {
  console.log(resp);

  // alert('Document uploaded successfully');
});

}

api服务器是:

public static function upload2(Request $request)
{
    $images = array();
    // $input=$request->all();
    if ($request->hasFile('photo')) {
        $files = $request->file("photo");
        foreach ($files as $file) {
            $name = $file->getClientOriginalName();
            $file->move(storage_path("/app/images/test"), $name);
            $images[] = $name;
        }

        DB::table('images')
            ->insert(['image' => $images]);
        $x = 1;
    } else {
        $x = 0;
    }
    return Response()->json(
        [
            "res" => $request,
        ],
        response::HTTP_OK
    );
}

0 个答案:

没有答案