无法上传图像(反应原生,拉拉维尔)



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

后端

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: "SymfonyComponentHttpFoundationFileExceptionFileNotFoundException", 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

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

你应该做这样的事情:

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'));

我不知道反应很好,但似乎您只传递图像属性,而不是以字节为单位传递图像文件或图像数据。

尝试更改代码:

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

对于此代码:

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

或者尝试将Content-Type更改为'multipart/form-data; boundary=${data._boundary}'

如果你的镜像是base64,那么你需要在API中进行一些更改,如下所示:

$path = 'bin_images/file_name.jpeg';
$image = str_replace('data:image/png;base64,', '', $request->image);
$image = str_replace(' ', '+', $image);
Storage::put($path, base64_decode($image));

最新更新