2016-11-10 2 views
0

私はこのようなファイルを返す私のLaravel 5.3アプリケーションでの方法を持っている:私はこのようなvue.jsでGETリクエストを作ってるんだLaravelファイルのダウンロードに

public function show(File $file) 
{ 
    $path = storage_path('app/' . $file->getPath()); 
    return response()->download($path, $file->name); 
} 

show (file) { 
    Vue.http.get('/api/file/' + file); 
} 
ここで間違っている可能性が何

enter image description here

結果がこれですか?私はブラウザが画像をダウンロードすることを期待しています。

--EDIT--

enter image description here

I dd($path);これが結果です:api.php/home/vagrant/Code/forum/storage/app/users/3/messages/xReamZk7vheAyNGkJ8qKgVVsrUbagCdeze.png

ルートがである:

Route::get('/file/{file}',        'File\[email protected]'); 

私はそれを追加した場合私のweb.phpは動作しています。しかし、私は私のAPIを介してアクセスする必要があります!あなたはこのようにそれを行うことができます

+0

第三引数として 'ダウンロード()'メソッドヘッダの –

+0

どのような種類のヘッダーを追加します。

public function show(File $file) { $path = storage_path('app/' . $file->getPath()); if(!file_exists($path)) throw new Exception("Can't get file"); $headers = array( "Content-Disposition: attachment; filename=\"" . basename($path) . "\"", "Content-Type: application/force-download", "Content-Length: " . filesize($path), "Connection: close" ); return response()->download($path, $file->name, $headers); } 

OR 使用カスタムダウンロード機能は次のようでなければなりませんか?私は彼らが既に含まれていると思った。 – Jamie

+0

あなたの質問に 'dd($ path)'を表示できますか? –

答えて

0

public function show(File $file) 
{ 
    $path = storage_path('app/' . $file->getPath()); 
    $headers = array(
     'Content-Type: image/png', 
    ); 
    return response()->download($path, $file->name, $headers); 
} 

は、この情報がお役に立てば幸い!

+0

ありがとう、私はまだ同じを受け取る! – Jamie

0

ヘッダを追加します。

function download($filepath, $filename = '') { 
    header("Content-Disposition: attachment; filename=\"" . basename($filepath) . "\""); 
    header("Content-Type: application/force-download"); 
    header("Content-Length: " . filesize($filepath)); 
    header("Connection: close"); 
    readfile($filepath); 
    exit; 
} 
+0

こんにちは、助けてくれてありがとう。私はそれをしました^。ネットワークタブの画像を見ると(上記の編集を参照)、ヘッダーが表示されます。しかし、それは働いていない:( – Jamie

+0

私は第二の選択肢を作ろうとしたのですか? まずファイルの存在をテストしなければならないと思いますが、正しいパスで画像にアクセスできますか? – Fky

+0

' '' api.php''の代わりに.php'''を実行していますが、私のapiからアクセスする必要があります。 – Jamie

関連する問題