0

I've got a web api controller that returns an .xlsx file. I'm adding the filename in the header like this:

response.Content.Headers.ContentDisposition = new    ContentDispositionHeaderValue("attachment")
{
   CreationDate = DateTime.Now,
   FileName = wbName
};

In Chrome it shows up like this:

Content-Disposition:attachment; creation-date="Tue, 08 Jul 2014 09:43:50 GMT"; filename="=?utf-8?B?   VW5rbm93biBXb3Jkcy1EZWNhcGl0YXRlZCBjaGFpbnNhdyBtdXJkZXIg4oCYdmljdGlt4oCZIG1heSBoYXZlIGtpbGxlZCBoaW1zZWx  mLCBjbGFpbXMgbGF3eWVyLTA4LzA3LzIwMTQgMTM6NDM6NTAueGxzeA==?="

However, when I download the file in Safari iOS it doesn't decode the file name, it just uses the huge encoded string for the name of the file. I'm going to try to add the file name to the URL and see if that works but I'd thought I'd ask for ideas here. How do I set the file name so Safari iOS recognizes it?

panchoLopez
  • 339
  • 3
  • 10

1 Answers1

0

When I added FileNameStar instead of FileName Mobile Safari stopped using the filename from the header and started looking at the url for the file name. Note that in this code response is an HttpResponseMessage object.

        response.Content.Headers.ContentDisposition = new    ContentDispositionHeaderValue("attachment")
        {
            CreationDate = DateTime.Now,
            // FileName = wbName,
            FileNameStar = wbName
        };

For completeness this is what FileNameStar returns in the header, which is the standard

Content-Disposition:attachment; creation-date="Tue, 08 Jul 2014 12:56:05 GMT"; filename*=utf-   8''Unknown%20Words-Decapitated%20chainsaw%20murder%20%E2%80%98victim%E2%80%99%20may%20have%20killed%20himself%2C%20claims%20lawyer-08%2F07%2F2014%2012%3A56%3A03.xlsx

So I started looking at how to get the filename in the response url. This did the trick

        UriBuilder ub = new UriBuilder(Request.RequestUri);
        ub.Path = fileName; // you should clean this up first eg no spaces or encode it
        response.Headers.Location = ub.Uri;

This cuts off everything in the path and returns just the base url with the filename on the end of it eg mydomain.com/filename.txt. This did the trick and now it looks like Safari iOS is happy and all the other browsers still use the content-disposition filename.

Community
  • 1
  • 1
panchoLopez
  • 339
  • 3
  • 10