Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
using Common;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;

namespace BootStrap.fa
{
    /// <summary>
    /// Summary description for ProductCatalogHandler
    /// 
    public class ProductCatalogHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            int productId;
            if (int.TryParse(context.Request.QueryString["ci"], out productId))
            {
                try
                {
                    using (var db = new Data.ModelContainer1())
                    {
                        var catalog = db.Catalogs.Where(c => c.Product != null && c.Product.Id == productId).FirstOrDefault();
                        if (catalog == null || catalog.CatalogFile == null)
                        {
                            var returnUrl = context.Request.QueryString["ReturnURL2"];
                            context.Response.Redirect(returnUrl);
                            return;
                        }
                        else
                        {
                            var content = (catalog.CatalogFileExt != null) ? catalog.CatalogFileExt : "application/pdf";
                            var filename = (catalog.CatalogFileName != null) ? catalog.CatalogFileName : "Catalog.pdf";
                            filename = filename.Replace(" ", "");

                            context.Response.Buffer = true;
                            context.Response.Charset = "";
                            context.Response.Clear();

                            context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
                            context.Response.AddHeader("Content-Length", catalog.CatalogFile.Length.ToString());
                            context.Response.AddHeader("Content-Disposition", "attachment; filename=" + filename);

                            MemoryStream memory = new MemoryStream(catalog.CatalogFile);
                            context.Response.OutputStream.Write(memory.ToArray(), 0, memory.ToArray().Length);

                            HttpContext.Current.Response.Flush(); HttpContext.Current.Response.Close(); HttpContext.Current.ApplicationInstance.CompleteRequest();
                        }
                    }
                }
                catch { }
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}


What I have tried:

When I download (open) pdf directly in Chrome or Firefox , browser show invalid code and in address bar show downloaded file with "htm" extensions.

Please Help me.
Thanks
Posted
Updated 13-Sep-17 12:39pm
v2

1 solution

I use the following to pass a Report from server to client - the report is a Crystal Report that is exported to a System.IO.Stream - which is basically what you have - and is successful on all browsers I have tested with;

C#
// create a byte array to store your memory stream
byte[] fileArray = new byte[memory.Length];
memory.Read(fileArray, 0, Convert.ToInt32(memory.Length));
// clear content & headers
context.Response.ClearContent();
context.Response.ClearHeaders();
// the actual string you require is ".pdf","application/pdf"
// The additional '\' are used to escape the quotes within the string
context.Response.ContentType = "\".pdf\",\"application/pdf\""
context.Response.AddHeader("Content-Length", catalog.CatalogFile.Length.ToString());
context.Response.AddHeader("Content-disposition", "attachment;filename=" + fileName);
context.Response.BinaryWrite(fileArray);
// end the response
context.Response.Flush();
context.Response.Clear();
context.Response.Close();
context.Response.End();


Hope this helps

Kind Regards
 
Share this answer
 
Comments
Member 11146269 19-Dec-18 16:00pm    
To Nader Barman, Graeme_Grant, or anOther1:
For those of us trying to understand this a bit further, could you provide context as to how you would implement the above code? Any additional information would help me paint a more clear picture.

Thank you.
an0ther1 2-Jan-19 17:26pm    
That depends on the technology you are using - ASP versus MVC.
You are probably better off raising a new question that commenting here

Kind Regards

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900