G--
G--

Reputation: 23

download multiple pdf

I am trying to download multiple pdf's as attachments in my asp.net application.I have created some templates and filling values using pdfstamper(itextsharp). I am able to fill the values but not able to download.

private void FillForm(string path, DataTable BridgeValues, DataTable Comments, DataTable Maintenance,string Newfilename)
    {
        try
        {
            string pdfTemplate = path;
            string newFile = Newfilename;
            string Pathser = "";
            if (!System.IO.Directory.Exists(Server.MapPath(@"~/PDF/")))
            {
                System.IO.Directory.CreateDirectory(Server.MapPath(@"~/PDF/"));
            }

            if (Directory.Exists(Server.MapPath(@"~/PDF/")))
            {
               Pathser = Server.MapPath(@"~/PDF/" + Newfilename);
            }
            System.IO.MemoryStream mStream = new System.IO.MemoryStream();
            // create a new PDF reader based on the PDF template document
            PdfReader pdfReader = new PdfReader(pdfTemplate);
            PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileStream(Pathser, FileMode.Create));
            AcroFields pdfFormFields = pdfStamper.AcroFields;
            DataColumn dc = null;
            for (int i = 0; i < BridgeValues.Columns.Count - 1; i++)
            {
                dc = BridgeValues.Columns[i];
                pdfFormFields.SetField(dc.ColumnName.ToString(),  BridgeValues.Rows[0][dc].ToString());
            }
            pdfStamper.FormFlattening = true;

            // close the pdf
            pdfStamper.Close();
            ////Response.ContentType = "application/octet-stream";
            Response.ContentType = "application/pdf";
            ////Response.AddHeader("Content-Disposition", "attachment; filename=Report.pdf");
            Response.AddHeader("Content-Disposition", "attachment; filename=" + Newfilename + "");
            ////Response.BinaryWrite(mStream.ToArray());
            Response.TransmitFile(Server.MapPath(("~/PDF/"+ Newfilename)));
            Response.Clear();
            Response.End();
        }
        catch (System.Threading.ThreadAbortException lException)
        {

            // do nothing

        }
    }

First time I tried to create one pdf ,it worked but later when I tried to download multiple files it gave an execption. Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack.

Upvotes: 0

Views: 2155

Answers (1)

Jakub Konecki
Jakub Konecki

Reputation: 46018

I don't think you can download multiple files by making one request and returning a collection from the action. I would suggest that it you need to allow user to download multiple files you ZIP them and stream the archive down to the browser.

Here is an example of zipping multiple files: http://devpinoy.org/blogs/keithrull/archive/2008/01/25/how-to-create-zip-files-in-c-with-sharpziplib-ziplib.aspx

Upvotes: 1

Related Questions