[2.0] Content-Disposition header is not being set on a file download

1,147 views
Skip to first unread message

Simon Robins

unread,
Apr 3, 2012, 6:34:31 AM4/3/12
to play-fr...@googlegroups.com

The following code works beautifully apart from the fact that the content-disposition header is never set and the file gets downloaded with an incorrect name.

I have scoured the Java APIs but cannot see a way to set the header. I have tried to set it in the apply function but the response object is null at that point. I have tried setting the header in the index function but it is ignored. Can someone explain where I am going wrong please? Many thanks.

package controllers;

import java.io.File;
import java.util.concurrent.Callable;

import play.libs.Akka;
import play.libs.F;
import play.libs.F.Function;
import play.mvc.Controller;
import play.mvc.Result;

public class Application extends Controller
{
    public static Result index()
    {
        F.Promise<File> promiseOfFile = Akka.future(new Callable<File>()
        {
            public File call()
            {
                return new File("temporaryfilename.txt");
            }
        });

        response().setHeader("Content-Disposition", "attachment; filename=\"actualfilename.txt\"");

        return async(promiseOfFile.map(new Function<File, Result>()
        {
            @Override
            public Result apply(File file)
            {
                return ok(file);
            }
        }));
    }
}

Kevin Bosman

unread,
Apr 11, 2012, 11:14:23 AM4/11/12
to play-fr...@googlegroups.com
> The following code works beautifully apart from the fact that the
> content-disposition header is never set and the file gets downloaded with an
> incorrect name.
>
> I have scoured the Java APIs but cannot see a way to set the header. I
> have tried to set it in the apply function but the response object is null
> at that point. I have tried setting the header in the index function but it
> is ignored. Can someone explain where I am going wrong please? Many thanks.

I do believe a fix may be in the pipeline for this Java problem (scala
does not exhibit the same problem due to some fundamental underlying
differences).
In the meantime, we've solved this problem as follows:

Within your async map Function, instead of directly returning
ok(file), you need to extract the wrapped scala result by calling
getWrappedResult() and cast it to play.api.mvc.PlainResult.

Once you have the underlying PlainResult, you can freely add headers,
flash, content-types, etc to it.

Now, because the PlainResult is immutable, when you're done you need
to *rewrap* it into a *new* play.mvc.Result Java object and return
that new instance from your async map Function.

Works like a charm :)

Guillaume Bort

unread,
Apr 11, 2012, 1:30:40 PM4/11/12
to play-fr...@googlegroups.com
There is a pull request fixing this that should be merged soon.

> --
> You received this message because you are subscribed to the Google Groups
> "play-framework" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/play-framework/-/vLHFKFu9du4J.
> To post to this group, send email to play-fr...@googlegroups.com.
> To unsubscribe from this group, send email to
> play-framewor...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/play-framework?hl=en.

--
Guillaume Bort

Mike

unread,
Feb 11, 2013, 1:13:58 PM2/11/13
to play-fr...@googlegroups.com
As far as I can see it does not work with 2.0.4.

Mike

unread,
Feb 11, 2013, 1:25:05 PM2/11/13
to play-fr...@googlegroups.com
Moreover the above workaround is for Scala developpers. Since I use the Java API I am stuck (to be polite; a french verb starting with the letter 'b' is what I have in mind).

I guess that nobody would want to use the Java API and also return a file content with a sensible name.

Niklas Nylund

unread,
Feb 11, 2013, 3:43:04 PM2/11/13
to play-fr...@googlegroups.com
I haven't tested but this should work in 2.1 if you just move the response().setHeader() call to inside the apply()-method, see https://groups.google.com/forum/#!topic/play-framework/-RlqeZDHcFU


Niklas
Reply all
Reply to author
Forward
0 new messages