2016-06-25 4 views
0

こんにちは私の質問は、Play Frameworkのダウンロードを処理する方法です。 私のコードはJAVAです。a-tag Play Frameworkを使用したファイルをダウンロード

User user = User.find.byId(Long.parseLong(id)); 
    File tempFile = null; 
    try { 
     tempFile = File.createTempFile(user.attachment_name, ".zip", null); 
     FileOutputStream fos = new FileOutputStream(tempFile); 
     fos.write(user.attachment); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

     return ok(tempFile); 

が、私は私はあなたがコンテンツをダウンロードするためのResult関数を作成する必要がリンク

答えて

1

でそれをダウンロードすることができますHTMLビュー収益のresonseをヘンデルできる方法を知ってはいけません。例えば:

public static Result download(String id) throws IOException { 
    User user = User.find.byId(Long.parseLong(id)); 
    File tempFile = null; 
    try { 
     tempFile = File.createTempFile(user.attachment_name, ".zip", null); 
     FileOutputStream fos = new FileOutputStream(tempFile); 
     fos.write(user.attachment); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

    return ok(tempFile); 
} 

ルート:GET /download controllers.Application.download(id)

アイテムのダウンロードリンクを持っている場合は、そのhrefhref="@routes.Application.download(id)"する必要があります。あなたは、あなたがContentTypeContent dispositionを追加する必要があるかもしれませんid

を使用できるように表示する(私はあなたがユーザーのIDを使用すると信じて)idを渡す必要があります。

response().setContentType("application/octet-stream"); 
response().setHeader("Content-disposition", "attachment; filename=yourFileName.pdf"); 

他のオプションは、再生フレームワークがserving filesをサポートしていることです。すでにフォルダ内にファイルがある場合は、パスを指定してフレームワークに処理させることができます。

public Result index() { 
    return ok(new java.io.File("/tmp/fileToServe.pdf")); 
} 
関連する問題