views:

646

answers:

4

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.

+1  A: 

Hi,

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.

LoSTxMiND
A: 

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.

Nick Berardi
A: 

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"

Chris
A: 

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

Taliesin