下载多个pdf

时间:2012-02-03 16:51:46

标签: c# asp.net

我正在尝试在我的asp.net应用程序中下载多个pdf作为附件。我使用pdfstamper(itextsharp)创建了一些模板和填充值。我能够填写值但无法下载。

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

        }
    }

我第一次尝试创建一个pdf,但它起作用但后来当我尝试下载多个文件时,它给出了一个execption。 无法计算表达式,因为代码已优化或本机框位于调用堆栈之上。

1 个答案:

答案 0 :(得分:1)

我不认为您可以通过发出一个请求并从操作中返回一个集合来下载多个文件。我建议你需要允许用户下载你压缩的多个文件,并将存档流式传输到浏览器。

以下是压缩多个文件的示例:http://devpinoy.org/blogs/keithrull/archive/2008/01/25/how-to-create-zip-files-in-c-with-sharpziplib-ziplib.aspx