【问题标题】:download multiple pdf下载多个pdf
【发布时间】:2012-02-26 06:51:22
【问题描述】:

我正在尝试在我的 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,它工作但后来当我尝试下载多个文件时它给出了一个执行。 无法计算表达式,因为代码已优化或本机框架位于调用堆栈顶部。

【问题讨论】:

  • 为什么要捕获 ThreadAbortException?它会自动重新抛出,但你不应该抓住它......至于例外 - 不会包含“无法评估表达式......”部分 - 那只是调试器显示的内容不是异常的一部分。
  • 那是我做的最后一次尝试,有一些执行“无法评估表达式,因为代码已优化或本机框架位于调用堆栈顶部。”所以我尝试了 catch 部分。
  • 不,这不是例外。真的,真的不是。这只是调试器中显示的内容。如果您查看消息的异常,则不是这样。

标签: c# asp.net


【解决方案1】:

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

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

【讨论】:

  • 我正在循环浏览文件数量并下载它们,但这是个坏主意。正如你所说,我会将它们作为 zip 下载。我可以在服务器上保存许多文件并将它们作为 zip 下载??有什么例子吗?
猜你喜欢
  • 2019-07-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-23
  • 2013-07-10
  • 2021-09-24
  • 1970-01-01
相关资源
最近更新 更多