我一直在開發一項服務,用戶只有在付費后才能訪問內容。所以我將內容上傳到存儲目錄并為了訪問內容我創建了這樣的路由..Route::any('storage/course/videos/{video_id}',['uses'=>'StorageController@video','middleware'=>'auth']);然后在存儲控制器中,我正在獲取文件并將其作為響應返回......public function video($video_id){ //Some logic for subscription check goes here $path = storage_path('app/public/course/videos/'.$video_id); // return $path; if (!File::exists($path)) { abort(404); } $file = File::get($path); $type = File::mimeType($path); $response = Response::make($file, 200); $response->header("Content-Type", $type); // $response->header("Accept-Ranges", 'bytes'); //$response->header("Content-Length", 51265590); // $response->header("Content-Disposition" ,"attachment"); //Triggers Download return $response; }這個技巧對圖像按預期工作,但對視頻無效。即使視頻下載工作正常,但瀏覽器每次都無法播放視頻。最初我認為可能缺少一些標題,但所以我嘗試了一些標題但它沒有工作.那么我做錯了什么或者是否有可能這樣做或任何其他技術來實現這一點
1 回答

慕斯709654
TA貢獻1840條經驗 獲得超5個贊
它在使用我從這里https://gist.github.com/vluzrmos/d5682ad426525196d069獲得的附加 VideoStream 類后工作
public function video($video_id){
$path = storage_path('app/public/course/videos/'.$video_id);
if (!File::exists($path)) {
abort(404);
}
$stream = new \App\Http\VideoStream($path);
return response()->stream(function() use ($stream) {
$stream->start();
});
}
- 1 回答
- 0 關注
- 103 瀏覽
添加回答
舉報
0/150
提交
取消