简体   繁体   中英

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

Why is this part of code is wrong? foreach loop in directory with-> file names and sizes and download link for each file.

When i ispect download button in chrome i got this in development tool:

<a href="#" class="btn btn-sm btn-outline-primary btn-block btn-login text-uppercase mb-2" 
    download="HTTP/1.0 200 OK
    0: Content-Type: application/octet-stream
    Cache-Control:       public
    Content-Disposition: attachment; filename=testfile.pdf;
    Date:                Fri, 27 Nov 2020 13:58:31 GMT
    Last-Modified:       Fri, 27 Nov 2020 13:58:31 GMT
 ">Download</a>

when it the button i download file with name:

HTTP_1.0 200 OK_0_Content-Type_ application_octet-stream_Cache-Control_public_.pdf__Date_                
   Fri, 27 Nov 2020 13_58_31 GMT_Last-Modified_Fri, 27 Nov 2020 13_58_31 GMT__    

insteed of testfile.pdf

code

   foreach($filesDownloadPath as $path) {

    $filesDownloads = pathinfo($path)['basename'];
    $filesDownloadSize = ConvertFileSize::ConversionFile(filesize($path));
    $downloadLink = response()->download(storage_path('app/Files/'.$filesDownloads), $filesDownloads, 
          $headers);
    }

    return view('page',["$filesDownloads" => 'fileDownloads',
                        "$filesDownloadSize" => 'filesDownloadSize',
                        "$downloadLink" => 'downloadLink'
    ]);
 

is problem with varibale $downloadLink ? with response()->download() ?

blade

@foreach($filesDownloadPath as $path) 
    <tr>
        <td>{{ $filesDownloads }}</td>
        <td>{{ $filesDownloadSize}}</td>
        <td><a href="#" class="btn btn-sm btn-outline-primary btn-block btn-login text-uppercase mb-2" download='{{ $downloadLink }}'>Download</a></td>
    </tr>
@endforeach

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, eg,

$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.

I download like this way

$file = storage_path()."app/Files/'.$filesDownloads";
if (file_exists($file)) {
    $headers = [
        'Content-Type' => 'application/pdf',
    ];
    return response()->download($file, "{$filesDownloads}", $headers);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM