0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[Laravel5.7]storageに保存した画像を指定してダウンロードする

Posted at

storageに保存した画像を指定してダウンロードする

ルーティング

web.php
Route::get('/download', 'PhotoController@download');

コントローラー

PhotoController.php
<?php

namespace App\Http\Controllers;
/* 以下略 */

    public function post(Request $request)
    {
        $post = new Photo();
        $post->title = $request->title; 
        //*画像の保存先 storage/app/public/ファイル名
        $post->file = $request->file->store('public'); 
        $post->file = str_replace('public/', '', $post->file);
        $post->user_id = Auth::user()->id;
        $post->save();

        return redirect('/');

    }
}
Controller.php
    public function download()
    {

        $headers = [
            'Content-Type' => 'application/octet-stream',
        ];
        return response()->download(storage_path('public/ディレクトリのファイル名'), $headers);
    }

###フロントサイド

blade.php
<a class="list--download list--icon" href="/download">

click. . . ダウンロード出来ました。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?