Download Files in Laravel Using Response::Download

Download files in laravel using Response::download

Try this.

public function getDownload()
{
//PDF file is stored under project/public/download/info.pdf
$file= public_path(). "/download/info.pdf";

$headers = array(
'Content-Type: application/pdf',
);

return Response::download($file, 'filename.pdf', $headers);
}

"./download/info.pdf"will not work as you have to give full physical path.

Update 20/05/2016

Laravel 5, 5.1, 5.2 or 5.* users can use the following method instead of Response facade. However, my previous answer will work for both Laravel 4 or 5. (the $header array structure change to associative array =>- the colon after 'Content-Type' was deleted - if we don't do those changes then headers will be added in wrong way: the name of header wil be number started from 0,1,...)

$headers = [
'Content-Type' => 'application/pdf',
];

return response()->download($file, 'filename.pdf', $headers);

Laravel response()- download(), not working correctly

response()->download() produces a Response object which must be returned from a controller to do anything. You'll need to return an array of files names or ids to your template, e.g.,

$filesForDownload = [];
foreach($filesDownloadPath as $path) {
$filesForDownload[] = [
'path' => pathinfo($path)['basename'],
'size' => ConvertFileSize::ConversionFile(filesize($path))
];
}

return view('page', ["filesForDownload" => $filesForDownload]);

then in your view:

@foreach($filesForDownload as $file) 
<tr>
<td>{{ $file['path'] }}</td>
<td>{{ $file['size'] }}</td>
<td><a href="/file/{{$file['path']}}" class="btn btn-sm btn-outline-primary btn-block btn-login text-uppercase mb-2">Download</a></td>
</tr>
@endforeach

Next we create a route in laravel pointing to a new method in your controller:

Route::get('/file/{filePath}', 'YourController@downloadFile');

and create a method in your controller which returns the response()->download()

public function downloadFile($filePath) {
return response()->download(storage_path('app/Files/'.$filePath), $filePath);
}

Note that this basic code has absolutely no security checking and could allow any user to download any file within app/Files (or anywhere on your system depending on your PHP config). You can get around that by having a whitelist of files which can be downloaded or storing file info in a database and have them provide the file ID to download it.

Download file in laravel

First you are not passing the name of the attachment from your View back to your controller so change your view to:

<!-- Inf File Field   -->
<div class="form-group">
{!! Form::label('inf_file', 'Attachements:') !!}
<a href="/download/{{ $infrastructure->inf_file }}">Download Now</a>
</div>

Then in your route you need to access the name of the file like so:

Route::get('/download/{Attachment}', function($Attachment){
$name = $Attachment;

$file = Storage::disk('public')->get("infrastructure/".$Attachment);

$headers = array(
'Content-Type: application/pdf',
);

return Response::download($file, $name, $headers);
});

How to download file from storage?

Remove this download="{{$file->name}}" from the link.

You can add download as html attribute:

<a href="{!! route('download', $file->name) !!}" download>{{ $file->name }}</a>

But you don't need it in this case, use just:

<a href="{!! route('download', $file->name) !!}">{{$file->name}}</a>

The response()->download() method in your controller will generate a response that forces the browser to download the file at the given path. So make sure your path i correct.

If your file is in your-project-root-path/storage/app/files/, you can use:

return response()->download(storage_path('/app/files/'. $file));

If your file is in your-project-root-path/storage/storage/app/files/, use:

return response()->download(storage_path('/storage/app/files/'. $file));

How to download file from external api response source using Laravel?

Issue is resolved.

I have used file_get_contents and file_put_contents built in function to get and store contents from api resource. Functionality is running fine now.

// URL src and Filename from API response
$fileSource = $jsonDecodedResults['result']['invoice']['src'];
$fileName = $jsonDecodedResults['result']['invoice']['filename'];
$headers = ['Content-Type: application/pdf'];

$pathToFile = storage_path('app/'.$fileName);
$getContent = file_get_contents($fileSource); // Here cURL can be use.
file_put_contents( $pathToFile, $getContent );
return response()->download($pathToFile, $fileName, $headers);

OR

We can use curl $getContent = $this->curl($fileSource); instead of $getContent = file_get_contents($fileSource);

public function curl($url) 
{
//create a new cURL resource
$ch = curl_init();

// TRUE to return the transfer as a string of the return value of curl_exec() instead of outputting it directly.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// set url and other appropriate options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);

// grab url and pass it to the browser
$result = curl_exec($ch);
// close cURL resouces, and free up system resources
curl_close($ch);

$jsonDecodedResults = json_decode($result, true);

return $jsonDecodedResults;
}


Related Topics



Leave a reply



Submit