Php Vue.js在laravel 5.8中获取公用文件夹图像
我正在创建评论系统,现在,我想在评论区显示用户配置文件图像 我想捕获并显示laravel 5.8公用文件夹中的多个图像。这是我试过的 如果用户有一个配置文件图像,我想显示它。如果不是,我想展示 使用vue化身组件的化身。我的图像存储在 公共/上传/配置文件 现在我的控制台中没有任何错误。我还可以显示用户名和用户评论 图像名称存储在mysql中 comment.vue(第一条评论) 配置文件表Php Vue.js在laravel 5.8中获取公用文件夹图像,php,laravel,vue.js,get,laravel-5.8,Php,Laravel,Vue.js,Get,Laravel 5.8,我正在创建评论系统,现在,我想在评论区显示用户配置文件图像 我想捕获并显示laravel 5.8公用文件夹中的多个图像。这是我试过的 如果用户有一个配置文件图像,我想显示它。如果不是,我想展示 使用vue化身组件的化身。我的图像存储在 公共/上传/配置文件 现在我的控制台中没有任何错误。我还可以显示用户名和用户评论 图像名称存储在mysql中 comment.vue(第一条评论) 配置文件表 Schema::create('profiles', function (Blueprint $tabl
Schema::create('profiles', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('user_id');
$table->string('image');
$table->string('gender');
$table->string('country');
$table->string('bod');
$table->string('instagram');
$table->string('description');
$table->timestamps();
});
在我的VS代码中,“上载”文件夹开始变为红色。我的解决方案是创建一个api路由以显示图像。比如说 api.php
Route::get('profile/{fileName}', 'ProfileController@showImage');
class ProfileController {
...
public function showImage($fileName)
{
$path = public_path("/images/{$fileName}");
if (!\File::exists($path)) {
return response()->json(['message' => 'Image not found.'], 404);
}
$file = \File::get($path);
$type = \File::mimeType($path);
$response = \Response::make($file, 200);
$response->header("Content-Type", $type);
return $response;
}
}
ProfileController.php
Route::get('profile/{fileName}', 'ProfileController@showImage');
class ProfileController {
...
public function showImage($fileName)
{
$path = public_path("/images/{$fileName}");
if (!\File::exists($path)) {
return response()->json(['message' => 'Image not found.'], 404);
}
$file = \File::get($path);
$type = \File::mimeType($path);
$response = \Response::make($file, 200);
$response->header("Content-Type", $type);
return $response;
}
}
您的图像src将是
/api/profile/{imageName}
。这是我的解决方案。检查您的img
src
标记,查看页面呈现后生成的url尝试使用前面的“/”/uploads/…
?在我的项目中,我总是需要前面的斜杠。“+item.image”看起来很有条理。@skribe我试过了,但它不起作用,正如@Vibha所说,最终呈现的src属性是什么样子的?谢谢,它不起作用,所以我更改了public_路径(“/images/{$fileName}”);通过公共路径(“/uploads/profile/{$fileName}”)。我现在的新错误是:8000/api/profile/[object%20Object]:1获取404(未找到)这个路径只是一个例子。你应该自己编辑正确的路径。是的,我认为我的公共路径是“/”+“上载”+“/”+“配置文件”+“/”+$fileName,所以我这样做了。但事实并非如此。无论如何,谢谢你的帮助。你在api.php
上创建了路由吗?还没有。我只有route::middleware('auth:api')->get('/user',function(Request$Request){return$Request->user();});