Jorge Lazo
Jorge Lazo

Reputation: 388

File download not working in Play Framework app deployed in Heroku

I have a button in a Play app to download a file, its a simple Action response:

def exportData(query: String): Action[AnyContent] = Action{ implicit request =>

    val file = new java.io.File("test.txt")
    val writer = new PrintWriter(file) //prepare the file + writer
    val data = SearchLib.get(query,request) //get data for file
    for(result <- data){ 
      //process looping through data, writing the lines to file
    }
    writer.close()

    Ok.sendFile( //send the file back to the user
      content = file,
      fileName = _ => query + "_search.txt",
      inline = false
    )
}

It works perfectly fine in a local host, but when I deploy it to Heroku and try it I get an application error with code H18 (server socket error)

2016-10-27T05:17:20.828591+00:00 app[web.1]: GET /export/nitrate
2016-10-27T05:17:21.383364+00:00 heroku[router]: sock=backend at=error code=H18 desc="Server Request Interrupted" method=GET path="/export/nitrate" host=pathway-query.herokuapp.com request_id=85a13382-5d50-4f8e-98d5-f60198083d9a fwd="174.6.50.44" dyno=web.1 connect=1ms service=506ms status=503 bytes=

Any other type of request or features in the app work fine. Any ideas why this might be?

Upvotes: 0

Views: 318

Answers (1)

Alexander Zaripov
Alexander Zaripov

Reputation: 36

If someone else reading this, I resolved this by using chunked responses rather than serving whole files - https://www.playframework.com/documentation/2.5.x/JavaStream#Chunked-responses by wrapping file to FileInputStream

Upvotes: 1

Related Questions