download excel-file Hi guy's,
I want to download an excel-file stored in storage/app/private/import via controller-function but everytime i open the downloaded-file I get an error: 'File-Type does not match file-extension'.
public function download($path)
{
$disk = Storage::disk('local');
return Storage::download($path);
}
I have this problem with .xls and .xlsx Files
Can you help me?
everytime i open the downloaded-file I get an error
Where is this error? In a browser, in Excel? Can you give a screenshot?
The error happens when opening the file in Excel.
„Excel cannot open the file 'corrupted.xlsx' because the file format or file extension is not valid. Verify that the file has not been corrupted and that the file extension matches the format of the file.“
If you get this file right from the storage (using SCP for example) is it opened fine?
I mean, is this file corrupted from the time of creation or is it corrupted during downloading? These are two different cases.
When I open the file directly on the server, everything works fine. So, the problem occurs during download.
Look in browser's dev tools which content-type header is used for download response. Maybe there is something wrong and browser breaks a file.
Try to add explicit content type:
$content_type = match(true) {
Str::endsWith($path, '.xls') => 'application/vnd.ms-excel',
Str::endsWith($path, '.xlsx') => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
};
return Storage::download($path, basename($path), [
'Content-type' => $content_type,
]);
Can you give both normal and broken file to compare?
Also there are bug reports describing the same behaviour as yours: https://github.com/laravel/framework/issues/56209
Which Laravel version you use?
I confirm XLSX file is downloaded fine on Laravel 12.50.0 + php artisan serve:
// routes/web.php
Route::get('test', function() {
return Storage::download('sample.xlsx');
});
http://127.0.0.1:8000/test gives me normal and readable xlsx file.
Response of the browser:
content-type: application/vnd.ms-excel
In my test-file there is only the word TEST
and in the broken file there are many symbols, numbers,...:
Calibri Light1,6¼)Calibri16¼)Calibri1Ü6¼)Calibri1Ü)Calibri1Ü)Calibri1Ü<)Calibri1Ü>)Calibri1Ü?
00_ ;_ *}-}
00_ ;_ *}-}
00_ ;_ *}-}
00_ ;_ *}-}
00_ ;_ *}-}
00_ ;_ *}-}
00_ ;_ *}-}
Can you share real files? Normal and downloaded broken one.
Please sign in or create an account to participate in this conversation.