Mathias F
Mathias F

Reputation: 15901

Firefox does not deliver pdf if it is handled by http-handler and inside iframe

I have a site that runs on IIS7 ASP.NET 3.5

I implemented a http-handler that serverves pdf.

If I request a pdf-document (www.mysite.com/mypdf.ashx?id=doc1) in Firefox 3.0 I get the result in the browser.

I now have an iframe on my page. The src - attribute is set to www.mysite.com/mypdf.ashx?id=doc1.

The document is displayed in IE7 but in Firefox I only get scrambled text. Is this posssible in Firefox?

I found this post PDF streaming in IFRAME not working in Firefox Anybody tried this solution with modrewrite? The post is a couple of years old and there was no modrewrite for IIS7 then.

Upvotes: 4

Views: 1776

Answers (4)

Taliesin
Taliesin

Reputation: 461

Serving PDFs with an http handler is tricky business. There are so many different ways of loading and reading a pdf. Different versions of Adobe Acrobat Reader behave different as well.

Sometimes it tries to be clever and see if it can use partial requests (206). So you can see the first page before its finished downloading the entire document. You probably want to also set the correct cache headers, as that will save you a lot of bandwidth.

I have been using this http handler to successfully serve pdfs without any problems. It takes cares of most of the hassles for you.

http://code.google.com/p/talifun-web/wiki/StaticFileHandler

Upvotes: 0

Chris
Chris

Reputation: 2045

Are you setting the ContentType for the Response? IE is pretty good at guessing what a document is; firefox relies on being told.

You'll want to set the ContentType to "application/pdf"

Upvotes: 0

Nick Berardi
Nick Berardi

Reputation: 54854

There was no mod_rewrite for IIS 6 either. It is an Apache only process. There are alternatives like IIS 7 Rewriter Module or Managed Fusion URL Rewriter.

Upvotes: 0

LoSTxMiND
LoSTxMiND

Reputation: 332

I do that in asp.net mvc and it works fine on IE6, IE7 and Firefox 3.5.3

Here is my code :

Html code :

<div id="ProductDetailsModal">
    <iframe src="<%= Url.Content("~/GetPdf.axd?id=" + ViewData["ProductId"] + "&type=" +  ViewData["ContentType"]) %>" width="100%" height="98%"></iframe>
</div>

And here the HttpHandler Code :

public void ProcessRequest(HttpContext context)
    {
        if (!String.IsNullOrEmpty(context.Request.Params["Id"]) && !String.IsNullOrEmpty(context.Request.Params["Type"]))
        {
            IProductBusiness productBusiness = new ProductBusiness();

            var username = context.Session["Username"] as String;
            var password = context.Session["Password"] as String;
            var id = context.Request.Params["Id"].ToInt();
            var type = context.Request.Params["Type"];

            if (id != 0 && !String.IsNullOrEmpty(type))
            {
                var pc = productBusiness.GetProductContent(username, password, id, type, string.Empty);

                if (!String.IsNullOrEmpty(pc.Name) && !String.IsNullOrEmpty(pc.Extension) && pc.Extension.ToLower() == "pdf")
                {
                    var len = pc.File.Length;
                    byte[] output = Convert.FromBase64String(pc.File);
                    context.Response.Clear();
                    context.Response.ContentType = "application/pdf";
                    context.Response.AddHeader("Content-Disposition", String.Format("FileName=\"{0}\"", pc.Name));
                    context.Response.AddHeader("content-length", output.Length.ToString());
                    context.Response.Cache.SetCacheability(HttpCacheability.Private);
                    context.Response.Expires = -1;
                    context.Response.Buffer = true;
                    context.Response.BinaryWrite(output);
                    context.Response.End();

                }
                else
                {
                    context.Response.Write("Erreur lors du chargement du fichier.");
                }
                context.Response.Clear();
            }
        }
    }

Hope this helps.

Upvotes: 1

Related Questions