0

这是让我恶心的代码行。

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

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

有任何想法吗?

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

4

4 回答 4

3

我用这个函数代替了任何 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;
}
于 2013-08-20T15:41:51.117 回答
1

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

文件路径可以这样实现:

public function getDownload(){

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

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

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

于 2013-12-06T04:36:52.647 回答
0

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

Route::get('file/download', function()
{
    $file = public_path(). '\download\myfile.png';
    return Response::download($file);
});
于 2014-05-20T11:25:31.750 回答
0

尝试在返回中包含 MIME:

$file = storage_path().'/file/' . $file->id . "." . $file->file->extension;
return Response::download($file, 200, array('content-type' => 'image/png'));
于 2013-07-31T22:21:41.807 回答