Rohit
Rohit

Reputation: 127

Downloadable xml files in Play Framework

I am a Scala/PlayFramework noob here, so please be easy on me :).

I am trying to create an action (serving a GET request) so that when I enter the url in the browser, the browser should download the file. So far I have this:

def sepaCreditXml() = Action {
  val data: SepaCreditTransfer = invoiceService.sepaCredit()
  val content: HtmlFormat.Appendable = views.html.sepacredittransfer(data)
  Ok(content)
}

What it does is basically show the XML in the browser (whereas I actually want it to download the file). Also, I have two problems with it:

  1. I am not sure if using Play's templating "views.html..." is the best idea to create an XML template. Is it good/simple enough or should I use a different solution for this?

  2. I have found Ok.sendFile in the Play's documentation. But it needs a java.io.File. I don't know how to create a File from HtmlFormat.Appendable. I would prefer to create a file in-memory, i.e. no new File("/tmp/temporary.xml").

EDIT: Here SepaCreditTransfer is a case class holding some data. Nothing special.

Upvotes: 1

Views: 304

Answers (1)

stefanobaghino
stefanobaghino

Reputation: 12804

I think it's quite normal for browsers to visualize XML instead of downloading it. Have you tried to use the application/force-download content type header, like this?

def sepaCreditXml() = Action {
  val data: SepaCreditTransfer = invoiceService.sepaCredit()
  val content: HtmlFormat.Appendable = views.html.sepacredittransfer(data)
  Ok(content).withHeaders("Content-Type" -> "application/force-download")
}

Upvotes: 1

Related Questions