我从API获得了JSON格式的响应,其中包含内容文件名、创建时间、用户id和URL。所以我想通过将URL放在<img src={props.generated} alt="image"/>
中,在前端显示响应中的图像。我的前端代码没有任何错误,但图像没有显示。
这是来自响应的图像URL
http://localhost:8000/storage/public/user_3/1594265090.jpeg
当我访问URL时,这里是浏览器中的响应
The requested resource /storage/public/user_3/1594265090.jpeg was not found on this server.
这是我的代码
$response = $this->client->request('POST', 'API', $data);
$image = $response->getBody()->getContents();
$filePath = 'public/' . $this->getUserDir();
$fileName = time().'.jpeg';
if ($result = Storage::put($filePath . '/' . $fileName, $image)) {
$generated = $model::create([
'name' => $fileName,
'file_path' => $filePath,
'type' => 'motif',
'customer_id' => Auth::id()
]);
return response($generated);
图像放在/storage/app/public
中,我相信这是因为目录的位置。我已经在尝试执行命令mklink /D "./storage/app/public" "./public/storage"
,我得到了响应Cannot create a file when that file already exists.
感谢您在这个问题上的建议。问题已经解决了。
我正在使用ln -rs ./storage/app/public ./public/storage
。但是这个命令是Linux,所以我在项目目录中使用git bash
并执行这个命令。
结果解决了,我可以访问图像。