首页 > 解决方案 > 无法上传图片(React Native,Laravel)

问题描述

我已经尝试了一段时间但没有成功

后端

public function createInspection(Request $request) {
   $path = Storage::disk('public')->putFile('bin_images', new File($request->file('image')));
   return $path;    
}

客户

data.append("image", {
  uri: this.state.images[0].path,
  type: this.state.images[0].mime,
  size: this.state.images[0].size,
  name: filename
});

this.props.sendInspection(data)

服务(缩短)

const res = await axios.post(url, data, {
    headers: {
      Authorization: `Bearer ${token}`,
      Accept: "application/json",
      "Content-Type": "multipart/form-data"
    }
  });

我不确定这是否与无法识别路径(ios图像路径)有关,例如路径: /private/var/mobile/Containers/Data/Application/B6FEB0FD-F9C8-4088-B15F-99D27A818C76/tmp/react-native-image-crop-picker/7BD81594-30E0-4E00-919C-4F353BBDE46F.jpg

我收到这个错误

message: "The file "" does not exist", exception: "Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException", file: "/Users/ysr/tza-project/vendor/symfony/http-foundation/File/File.php", line: 37, file: "/Users/ysr/tza-project/vendor/symfony/http-foundation/File/MimeType/MimeTypeGuesser.php", line: 116

标签: ioslaravelreact-nativeaxios

解决方案


问题似乎出在您的后端,请查看文档

你应该这样做:

public function update(Request $request)
{
    $path = $request->file('image')->store('images');

    return $path;
}

如果您想使用putFile,则可以使用:

$path = Storage::putFile('images', $request->file('image'));

如果需要更换磁盘,则:

$path = Storage::disk('public')->putFile('images', $request->file('image'));

推荐阅读