Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Content-Disposition inline filename issue with IE

I am displaying a pdf in browser with inline from API using an aspx page.

While saving the pdf using Chrome/Firefox, takes the filename from
header("Content-Disposition", "inline;filename=xyz.pdf")

But while saving the pdf using IE it does not reads the filename from
header("Content-Disposition", "inline;filename=xyz.pdf").
instead it takes the aspx name.

Technical details

I have an xyz.aspx page. The xyz.aspx page will invoke an API for a document. Then the downloaded document from API will transferred to browser with inline to display the pdf document. Am setting the response header as below and writing the file bytes.

            HttpContext.Current.Response.ClearHeaders();

            Response.AddHeader("Content-Disposition", "inline;filename=\"" + Name + "\"");

            HttpContext.Current.Response.ContentType = "application/pdf";
  • Issue:

    While saving the opened pdf in IE it takes xyz.aspx instead of the name from header.

  • Requirement:

    While saving the pdf using IE, it need to save with the name of pdf.

I googled so much, as every one tells its IE behavior. I hope some one knows a solution.

Note: I have to display the pdf in browser and then save. Not to download using "attachment"

like image 530
Ragavan Avatar asked Mar 23 '16 12:03

Ragavan


People also ask

What does content-disposition inline mean?

Content-Disposition takes one of two values, `inline' and `attachment'. 'Inline' indicates that the entity should be immediately displayed to the user, whereas `attachment' means that the user should take additional action to view the entity.

What is content-disposition attachment filename?

Content-Disposition: attachment; filename=FILENAME. The filename parameter can be used to suggest a name for the file into which the resource is downloaded by the browser.


2 Answers

It is true some versions of IE can't handle ("Content-Disposition", "inline;filename=...")

This is because filename=... was originally intended for the attachment disposition. Not all browser-based PDF viewers can handle it.

The only solution I see is to allow access via a different url. Suppose you have a route to the pdf like: /pdf/view. If you change it to /pdf/view/filename and you configure your application to handle this route in the same way as /pdf/view your problem is solved.

You can also re-write the download url on the webserver. Depending on your webserver you have various ways of doing this.

like image 87
Hikariii Avatar answered Oct 20 '22 00:10

Hikariii


I have also tried with all kind of headers and methods.

In the end, my solution was

    private FileResult ReturnStreamAsPdf(string fileName, Stream stream)
    {
        ContentDisposition cd = new ContentDisposition
        {
            FileName = fileName,
            Inline = true  // false = prompt the user for downloading;  true = browser to try to show the file inline
        };
        Response.Headers.Add("Content-Disposition", cd.ToString());
        Response.Headers.Add("X-Content-Type-Options", "nosniff");

        return new FileStreamResult(stream, MediaTypeNames.Application.Pdf);
    }

and the Route Attribute on the method:

    [Route("api/getpdfticket/{ticketnumber}")]        
    public async Task<FileResult> GetPdfTicket(string ticketnumber)

And the href: href="@($"/api/getpdfticket/{product.TicketNumber}.pdf")"

It seems like Microsloft is still inventing their own standards: http://test.greenbytes.de/tech/tc2231/#inlwithasciifilenamepdf

PDF Handler : content-disposition filename

like image 22
Radek82 Avatar answered Oct 20 '22 02:10

Radek82