欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

laravel storage::disk download response 响应 下载

L5 Download using FileSystem

PUBLISHED 2 YEARS AGO BY RGIAVITI

Hey guys, I am trying to download a XLSX file using FileSystem but no success yet. I put my file in storage/app/sheets/models/sheet1.xlsx. In the controller, I tried:

    public function downloadSheet() {
        return Storage::download('sheets/models/sheet1.xlsx');
    }

However, when I try to download the file, what I get is a blank screen. No errors, no log errors... Nothing.

Best Answer(As Selected By rgiaviti)
willvincent

Hmm.. alright, well in that case, add this header to set the filename:

header('Content-Disposition: attachment; filename="sheet1.xlsx"');

So all together it should be:

$file = Storage::disk('local')->get('sheets/models/sheet1.xlsx');
return (new Response($file, 200))
    ->header('Content-Type', 'application/vnd.ms-excel')
    ->header('Content-Disposition: attachment; filename="sheet1.xlsx"');

If you can get the filesize, you may also want to include a Content-Length header.

willvincent

Do you have debug mode on in your settings? You're probably running into a fatal error, I don't believe download is a method available on that object.

Storage also isn't a response object.

Try something more like this:

public function get($filename){
 
 $entry = Fileentry::where('filename', '=', $filename)->firstOrFail();
 $file = Storage::disk('local')->get($entry->filename);
 
 return (new Response($file, 200))
              ->header('Content-Type', $entry->mime);
 }

See this for more.

 
rgiaviti

It worked!

$file = Storage::disk('local')->get('sheets/models/sheet1.xlsx');
return (new Response($file, 200))->header('Content-Type', 'application/vnd.ms-excel');

But how can I set the filename? When I download, I am getting the action name as filename and no extension (should be xlsx).

 
willvincent

Just took a quick look at the laravel docs. It'd be easier for you to just do this:

return Response::download('sheets/models/sheet1.xlsx');

Not sure how good the quality of code on that other site is. ;)

 
rgiaviti

@willvincent, this doesn't work for files in Storage (or I am doing something wrong). Maybe in public directory: public/files/test.txt. If I try this code I get FileNotFoundException. I also tried prepending with storage_path() function. But doesn't work too.

 
willvincent

Hmm.. alright, well in that case, add this header to set the filename:

header('Content-Disposition: attachment; filename="sheet1.xlsx"');

So all together it should be:

$file = Storage::disk('local')->get('sheets/models/sheet1.xlsx');
return (new Response($file, 200))
    ->header('Content-Type', 'application/vnd.ms-excel')
    ->header('Content-Disposition: attachment; filename="sheet1.xlsx"');

If you can get the filesize, you may also want to include a Content-Length header.

 
rgiaviti

Awesome. Just one more question. Do you know how to get the filename with Storage? I tried with:

Storage::disk('local')->name('sheets/models/sheet1.xlsx');

But this method does not exist. I took a look at Laravel API and I found the method here: http://laravel.com/api/5.0/Illuminate/Filesystem/Filesystem.html

However, if I take a look in Illuminate\Filesystem\FilesystemAdapter the method name() is not there, so I am little confused here.

 
willvincent

Technically, you're already passing a filename to the get method.. so you should already know it.

If what you're really after is how to get the filename off that string you've been using in your example, then just explode the string on / and grab the last element.

$parts = explode('/', 'sheets/models/sheet1.xlsx');
$filename = array_pop($parts);
 

来自 https://laracasts.com/discuss/channels/general-discussion/l5-download-using-filesystem

 

response download with file from storage

PUBLISHED 1 YEAR AGO BY JAHELLER

How can I response with a download in laravel using the result of

$file = Storage::disk('local')->get('image.jpg');

as downloadable file? The get() returns the file in raw, but using

return response()->download($pathToFile, $name, $headers);

just accepts a path to file. Because I would like to work with local and cloud storage I need to find a way to retrieve files from cloud storage and return them as download to the user. Any idea?

pmall
 pmall
1 year ago(575,095 XP)

I know it is a problem but there is no other solution than putting the path of the file. So when the file is uploaded you have to store the path of the file.

 
jaheller

:/ Ohh that's very bad. Any idea why the path doesn't work? my local storage config:

'local' => [
        'driver' => 'local',
        'root'   => storage_path('app/uploads'),
    ],

I use this in controller:

        return response()->download(storage_path() . $ticket_reply->file->name);

results in

The file "C:\xampp\htdocs\projekt\storageasasass-1453144780.jpg" does not exist

the filename is correct. What's wrong, seems like laravel ignores my config completly.

 
pmall
 pmall
1 year ago(575,095 XP)

The root of your disk is storage_path('app/uploads'), so you have to put storage_path('app/uploads') instead of storage_path() in your response()->download() call.

 
TerrePorter

This will return the full path to the requested file,

 Storage::disk($disk)->getDriver()->getAdapter()->applyPathPrefix($file)
 

来自  https://laracasts.com/discuss/channels/laravel/response-download-with-file-from-storage?page=1


普通分类: