为什么 Response::download() 在 Laravel 4 中不会下载除 PDF 之外的任何内容?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17973064/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 08:12:43  来源:igfitidea点击:

Why Response::download() won't download anything except PDF in Laravel 4?

phpdownloadlaravellaravel-4

提问by Harry Martland

This is the line of code which is giving me sick.

这是让我生病的代码行。

return Response::download(storage_path().'/file/' . $file->id . "." . $file->file->extension);

The files are uploaded and given an id which they are saved under e.g. 25.pdfthis works fine if the file is a PDF but doesn't for anything else e.g. PNG. we upgraded from Laravel 3 to 4 to try to overcome this problem.

文件被上传并给出一个 id,它们被保存在例如,25.pdf如果文件是 PDF,这可以正常工作,但不适用于其他任何东西,例如 PNG。我们从 Laravel 3 升级到 4 试图克服这个问题。

Any ideas?

有任何想法吗?

EDIT: I just uploaded a test text file with the word test in it once I uploaded it and then downloaded it I opened it, there were 3 blank lines and the letters te!!!!!I downloaded it through sftp and the file is correctly stored on the server so it is defiantly the download procedure!

编辑:我刚刚上传了一个测试文本文件,其中包含 test 这个词,然后我打开它,有 3 个空行和字母 te!!!! 我通过 sftp 下载了它,文件是正确存储在服务器上,因此它绝对是下载过程!

回答by Harry Martland

I used this function instead of any of the Laravel stuff. :/ (Stolen from other places around the web)

我使用这个函数而不是任何 Laravel 的东西。:/(从网络其他地方偷来的)

public static function big_download($path, $name = null, array $headers = array()) {
    if (is_null($name))
        $name = basename($path);
    $finfo = finfo_open(FILEINFO_MIME_TYPE);

    $pathParts = pathinfo($path);
    // Prepare the headers
    $headers = array_merge(array(
        'Content-Description' => 'File Transfer',
        'Content-Type' => finfo_file($finfo, $path),
        'Content-Transfer-Encoding' => 'binary',
        'Expires' => 0,
        'Cache-Control' => 'must-revalidate, post-check=0, pre-check=0',
        'Pragma' => 'public',
        'Content-Length' => File::size($path),
        'Content-Disposition' => 'inline; filename="' . $name . '.' . $pathParts['extension'] . '"'
            ), $headers);
    finfo_close($finfo);

    $response = new Symfony\Component\HttpFoundation\Response('', 200, $headers);

    // If there's a session we should save it now
    if (Config::get('session.driver') !== '') {
        Session::save();
    }

    // Below is from http://uk1.php.net/manual/en/function.fpassthru.php comments
    session_write_close();
    ob_end_clean();
    $response->sendHeaders();
    if ($file = fopen($path, 'rb')) {
        while (!feof($file) and (connection_status() == 0)) {
            print(fread($file, 1024 * 8));
            flush();
        }
        fclose($file);
    }

    // Finish off, like Laravel would
    Event::fire('laravel.done', array($response));
    $response->foundation->finish();

    exit;
}

回答by star18bit

One may ask, How can i get path to file in laravel?

有人可能会问,如何在laravel 中获取文件路径?

Path to file can be achieved like:

文件路径可以这样实现:

public function getDownload(){

        $file = public_path()."/downloads/info.pdf";
        $headers = array('Content-Type: application/pdf',);
        return Response::download($file, 'info.pdf',$headers);
    }

function will download file from : 'project/public/download' folder.

函数将从:'project/public/download' 文件夹下载文件。

(don't forget to set-up routes and controller by your self)

(不要忘记自己设置路由和控制器)

回答by Shaahin Ashayeri

If you are using Windows, go to php.ini and then uncomment "extension=php_fileinfo.dll" section and then use this code:

如果您使用的是 Windows,请转到 php.ini,然后取消注释“ extension=php_fileinfo.dll”部分,然后使用以下代码:

Route::get('file/download', function()
{
    $file = public_path(). '\download\myfile.png';
    return Response::download($file);
});

回答by Laurence

Try including the MIME in the return:

尝试在返回中包含 MIME:

$file = storage_path().'/file/' . $file->id . "." . $file->file->extension;
return Response::download($file, 200, array('content-type' => 'image/png'));