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 に答える
0

Windows を使用している場合は、php.ini に移動し、「extension=php_fileinfo.dll」セクションのコメントを外してから、次のコードを使用します。

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 に答える