【问题标题】:Force download .xls file in Laravel 4.2在 Laravel 4.2 中强制下载 .xls 文件
【发布时间】:2016-02-04 12:53:00
【问题描述】:

我有以下路线:

Route::get('/download/{id}/{filename}', function($id, $filename)
    {
        // Check if file exists in app/storage/file folder
        $file_path = storage_path() .'/orders/'.'/'.$id.'/'. $filename;
        if (file_exists($file_path))
        {

            // Send Download
            return Response::download($file_path, $filename, [
                'Content-Type: application/vnd.ms-excel; charset=utf-8'
            ]);
        }
        else
        {
            // Error
            exit('Requested file does not exist on our server!');
        }
    })->where('filename', '[A-Za-z0-9\-\_\.]+');

在 Laravel 中下载一个 excel 文件形式 /app/storage (之前上传)。 我面临的问题是下载的excel文件无法被excel读取。如果我从 FTP 下载上传的文件就可以了。我为 Content-Type 尝试了所有类型的标题,但似乎没有任何效果。 excel文件有特殊的标题吗? 非常感谢。

【问题讨论】:

    标签: php excel laravel


    【解决方案1】:

    我在 CI 中使用此代码,请查找 Laravel 的帮助文件或库

    $this->load->helper('download');
    if(file_exists("user_files/test.txt")){
        $data = file_get_contents("user_files/test.txt");
        force_download("test.txt", $data);
    }else{
        echo "Error";
    }
    

    【讨论】:

      猜你喜欢
      • 2021-07-31
      • 2014-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多