how to create and download a file using play framework?

Viewed 2035

In my scala app I have an api method I want to use to a click on a button this api method will be triggered and a file that im creating on the spot will be downloaded to the client computer from the browser.

This is my api method:

def downloadFile = Action {

    // creating a new empty file
    val file = new File("outputFile.csv")

    // creating a write that opens the file
    val writer = CSVWriter.open(file)

    // writing to the file
    writer.writeAll(**some csv content**)

    writer.close()

    Ok.sendFile(file, inline = false, _ => file.getName)
  } 

this works, but the problem is that it downloading the file to the project directory folder...instead of downloading the file from the browser to my computer as I want it to be.

1 Answers
Related