lina
lina

Reputation: 75

play framework downloading file

I am new in play framework I want download a file in the following way:

Is it possible to do in play framework?if yes how to think about give me some idea please..

Thanks

Upvotes: 0

Views: 4866

Answers (4)

Robin Raju
Robin Raju

Reputation: 1325

Downloading a file can be done easily using play controller as follows,

public Result downloadFile(){
    File fileToDownload = new File("/path/fileToDownload.pdf"):

    response().setContentType("application/x-download");
    response().setHeader("Content-disposition", "attachment; filename=fileToDownload.pdf");

    return ok(fileToDownload);
}

The Scala Way

def downloadFile = Action {
  Ok.sendFile(
  content = new java.io.File("/path/fileToDownload.pdf"),
  fileName = _ => "fileToDownload.pdf"
  )
}

Upvotes: 6

westsoldier
westsoldier

Reputation: 26

an example

in model with JPA

@Lob
@Column(name="NY_FILE",length=100000, nullable=false)
private byte[] myFile;

@Column(name="MIME_TYPE", nullable=false)
private String mimeType;

Controller

@play.db.jpa.Transactional(readOnly=true)
public Result download(Long id){
    myPOJO = arquivo.findByid(JPAapi.em(), id);
    return ok(MyPOJO.getMyFile()).as(myPOJO.getMimeType());
}

routes file

GET     /download/:id   @controllers.MyController.download(id:Long)

HTML link

<a id="linkDownload" href="/download/@myPOJO.getMyFile.getId" target="_blank">
   @myPOJO.getMyFile.getName
</a>

Upvotes: 1

Ramji Gupta
Ramji Gupta

Reputation: 11

File file = new File("file pathfr");
return ok(file);

Upvotes: 0

Codemwnci
Codemwnci

Reputation: 54924

If you mean the file path on the server, then this is trivial. Just use the renderBinary method. This can take an input stream or a File object, so ...

renderBinary(new File(filepath));

should do the trick.

Upvotes: 2

Related Questions