Frage

I have a akka actor that generates a bunch of files then zips them up, and returns that zip file. I want to then pass this file back to the complete method of spray routing to allow downloading of the zip file.

I would really like to be able to use getFileFrom, but I can't seem to figure out a way to utilize that with the future returned. I have also tried passing the file back to the complete function, but get a very cryptic error.

How do I chain futures to get the result I want to be downloaded in the browser?

Here is some of the code I am working with

get {
    path("test") {
      respondWithMediaType(`application/zip`) {
        complete {
          val pi = new PackageInfo("Test")
          doCreate(pi)
        }
      }
    }
  }

  def doCreate(pi: PackageInfo) = {
    val response = (worker ? Create(pi))
      .mapTo[Ok]
      .map(result => result.zipFile: File)
      .recover { case _ => "error" }

       response
  }

One of the many errors recieved while trying different things

could not find implicit value for parameter marshaller: spray.httpx.marshalling.ToResponseMarshaller[scala.concurrent.Future[Comparable[_ >: java.io.File with String <: Comparable[_ >: java.io.File with String <: java.io.Serializable] with java.io.Serializable] with java.io.Serializable]]
War es hilfreich?

Lösung

There are a few things to address here.

One is that after creating that file and sending it to the client you probably want to delete it. If you simply send it with getFromFile (doc) then you will not know when the client finished downloading the file and won't be able to delete that file. Here is how you can implement that functionality: Is it possible to install a callback after request processing is finished in Spray?.

The error with missing implicit means that Spray needs to know how to marshal your data back to the client. By default it can marshal only basic types like Strings, but you can implement your own custom marshallers. If you use approach described in the link I provided then you don't need to worry about that.

Finally that answer also provides a chunked response which is useful if your files are large enough.

If you decide to go with getFromFile look at this answer: Spray send xls file to client. I think that's exactly what you need.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top