Laravel 5 Flysystem - download file from remote di

2019-04-08 07:42发布

I am storing files for a site on Rackspace using Flysystem. Uploading is no problem, having trouble figuring out how to start a download for a file - this is what I have tried

Storage::disk('rackspace');
return response()->download('file-library/' . $file->filename);

The result is that the file could not be found. Is adding Storage::disk() sufficient for making Laravel look in this location rather than locally? What is the best way to accomplish this?

2条回答
Summer. ? 凉城
2楼-- · 2019-04-08 08:14

Frank here from Flysystem.

The preferred way to do this would be to use the readStream output in combination with Response::stream.

<?php

$fs = Storage::disk('diskname')->getDriver();
$stream = $fs->readStream($file);

return Response::stream(function() use($stream) {
    fpassthru($stream);
}, 200, [
    "Content-Type" => $fs->getMimetype($file),
    "Content-Length" => $fs->getSize($file),
    "Content-disposition" => "attachment; filename=\"" . basename($file) . "\"",
]);

The $fs is the League\Flysystem\Filesystem instance. I believe there is a method to retrieve this instance in the filesystem class Laravel provides.

查看更多
Deceive 欺骗
3楼-- · 2019-04-08 08:30

Is adding Storage::disk() sufficient for making Laravel look in this location rather than locally?

No, that wouldn't affect response()->download() calls.

Something like this should work:

return response()->download(Storage::disk('rackspace')->get('file-library/' . $file->filename));
查看更多
登录 后发表回答