我正在開發 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');
}
}
uj5u.com熱心網友回復:
絕對路徑(檔案系統路徑)不適用于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屬性。
uj5u.com熱心網友回復:
控制器
public function store(Request $request)
{
$validator = Validator::make($request->all(), [
'name' => 'required|string|max:100',
'description' => 'nullable|string',
'fileSource' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048'
]);
if ($validator->fails()) {
$errors = $validator->errors();
return response()->json($errors);
}
$image = $request->file('fileSource');
$imageName = $image->getClientOriginalName();
$destinationPath = public_path('storage');
$image->move($destinationPath, $imageName);
Destination::create([
'name' => $request->name,
'description' => $request->description,
'image' => $image
]);
return response()->json('Destination Created Successfully');
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/492975.html
下一篇:org.springframework.dao.IncorrectResultSizeDataAccessExceptionMongoLimit不起作用?
