Laravel 从存储返回图像链接,但链接不起作用

奥萨马·阿姆

我正在开发 Laravel 项目 API,我正在使用Storage来存储图像,我需要检索链接并将链接发送到前端,但是当我使用srcimg 属性中的链接时它不起作用

$destination = Destination::findOrFail($id);

$destination_images = $destination->destination_images;

$urls = [];

foreach ($destination_images as $destination_image) {
    $link = Storage::disk('public')->path($destination_image->img);
    array_push($urls, $link);
}

return response()->json($url);

这是链接数组

[
    "C:\\xampp\\htdocs\\rindextwo\\storage\\app/public\\destination-F2QSioUxe2.jpeg",
    "C:\\xampp\\htdocs\\rindextwo\\storage\\app/public\\destination-vRvimgsiMJ.png"
]

当我在浏览器中输入链接时,它会成功显示图像

但是当我在 Angular 项目中使用它时它不起作用

在将链接传递到前端之前,我必须完成什么或过程吗?

这是我在浏览器搜索栏中手动输入图像链接时对图像的浏览器开发工具检查

浏览器检查

整个控制器代码

<?php

namespace App\Http\Controllers\admin\dashboard\setup\locations;

use App\Http\Controllers\Controller;
use App\Models\admin\dashboard\setup\locations\Destination;
use App\Models\admin\dashboard\setup\locations\DestinationImage;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Str;

class ApiDestinationController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $destinations = Destination::get();

        return response()->json($destinations);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'name' => 'required|string|max:100',
            'description' => 'nullable|string',
            'fileSource' => 'required'
        ]);

        if ($validator->fails()) {
            $errors = $validator->errors();
            return response()->json($errors);
        }

        $destination = Destination::create([
            'name' => $request->name,
            'description' => $request->description
        ]);

        foreach ($request->fileSource as $img) {
            $extension = explode('/', explode(':', substr($img, 0, strpos($img, ';')))[1])[1];
            $replace = substr($img, 0, strpos($img, ',')+1);
            $image = str_replace($replace, '', $img);
            $image = str_replace(' ', '+', $image);
            $imageName = 'destination-' . Str::random(10).'.'.$extension;
            Storage::disk('public')->put($imageName, base64_decode($image));
            DestinationImage::create([
                'destination_id' => $destination->id,
                'img' => $imageName
            ]);
        }

        return response()->json('Destination Created Successfully');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $destination = Destination::findOrFail($id);

        $destination_images = $destination->destination_images;

        $urls = [];

        foreach ($destination_images as $destination_image) {
            //$link = Storage::disk('public')->path($destination_image->img);
            $link = public_path('storage/'.$destination_image->img);
            array_push($urls, $link);
        }

        $response = [];

        array_push($response, $destination, $urls);

        return response()->json($response);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $validator = Validator::make($request->all(), [
            'name' => 'nullable|string|max:100',
            'description' => 'nullable|string'
        ]);

        if ($validator->fails()) {
            $errors = $validator->errors();
            return response()->json($errors);
        }

        $destination = Destination::findOrFail($id);

        $destination->update([
            'name' => $request->name,
            'description' => $request->description
        ]);

        return response()->json('Destination Updated Successfully');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $destination = Destination::findOrFail($id);

        $destination->delete();

        return response()->json('Destination Deleted Successfully');
    }
}
唐卡纳什

绝对路径(文件系统路径)不适用于img标签上的src属性。

以下将始终返回绝对(文件系统)路径

Storage::disk('public')->path($destination_image->img);

//Or

public_path('storage/'.$destination_image->img);

要获取相对路径,您可以

$link = Storage::disk('public')->url($destination_image->img);

Laravel Docs - 文件系统 - 文件 URL

或者使用资产助手获取完全合格的 url

$link = asset('storage/' . trim($destination_image->img, '/'));

Laravel 文档 - 助手 - 资产

或者使用url helper 来获取完全限定的 url

$link = url('storage/' . trim($destination_image->img, '/'));

Laravel 文档 - 助手 - url

对于您的特定用例,您可以在DestinationImage模型上定义一个访问器,例如

class DestinationImage extends Model
{
    protected $appends = ['src'];

    public function getSrcAttribute()
    {
        return Storage::disk('public')->url($this->img);
    }

    //rest of the model code
}

然后在控制器中你可以急切地加载关系

public function show($id)
{
    $destination = Destination::with('destination_images')->findOrFail($id);

    return response()->json($destination);

    //Or since Eloquent models are Jsonable
    //return $destination;
}

并且您可以在前端访问destination_images记录上的src属性。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章