【问题标题】:Generating a PDF File using MVC使用 MVC 生成 PDF 文件
【发布时间】:2018-05-12 06:50:20
【问题描述】:

我正在尝试使用 MVC 生成 PDF,我遇到的问题是我不确定如何在最后显示或使其工作,我正在使用 ActionResult 执行存储过程并填充一个带有结果的数据表,我后来“尝试”在 PDF 生成器中使用它,到目前为止一切顺利,问题是当我将结果发送到视图时,我得到一个错误并且不知道如何解决它,这是一种生活状况,所以如果你能帮助我,我将非常感激,对不起我的英语不好。

动作结果:

public ActionResult PDFGenerator(string id
        )

    {
        using (Document document = new Document())
        {
            string idpago = "2";
            //while (id != null)
            int identificacionpago = Convert.ToInt32(idpago);
            DataTable dt = new DataTable();
            Database conex = Conexion.getInstancia();
            dt = conex.ExecuteDataSet("Usp_TraerPago", identificacionpago).Tables[0];
            string pago = dt.Rows[0]["ValorPago"].ToString();
            string aniopago = dt.Rows[0]["AnioPago"].ToString();
            DateTime fechapago = Convert.ToDateTime(dt.Rows[0]["FechaPago"]);
            //int pag = Convert.ToInt32(ViewBag.datos);
            int idusuario = Convert.ToInt32(dt.Rows[0]["IdUsuario"].ToString());

            DataTable dt1 = new DataTable();
            dt1 = conex.ExecuteDataSet("Usp_UsuarioPago", idusuario).Tables[0];
            string Numid = dt1.Rows[0]["NumIdentificacion"].ToString();
            string Tipoid = dt1.Rows[0]["TipoIdentificacion"].ToString();
            string Nombre = dt1.Rows[0]["NombresUsuario"].ToString();
            string apellidos = dt1.Rows[0]["ApellidosUsuario"].ToString();
            MemoryStream workStream = new MemoryStream();
            // Document document = new Document();
            PdfWriter.GetInstance(document, workStream).CloseStream = false;

            document.Open();

            document.Add(new Paragraph("                                                                                                                     " + DateTime.Now.ToString()));
            document.Add(new Paragraph("Certificado de participación"));
            document.Add(new Paragraph("  "));
            document.Add(new Paragraph("  "));

            document.Add(new Paragraph("                               por el año " + aniopago));
            document.Add(new Paragraph("El suscrito a  " + Nombre + " " + apellidos + " identificado con " + Tipoid + " " + Numid + " " + " En la fecha " + fechapago.Year + "/" + fechapago.Month + "/" + fechapago.Day + aniopago));
            document.Add(new Paragraph("  "));
            document.Add(new Paragraph("  "));
            document.Add(new Paragraph("  "));
            document.Add(new Paragraph("  "));
            document.Add(new Paragraph("  "));
            document.Add(new Paragraph("  "));
            document.Add(new Paragraph("  "));
            document.Add(new Paragraph("  "));
            document.Add(new Paragraph("___________________________ "));
            document.Add(new Paragraph("Firma del Revisor Fiscal   "));

            //document.SaveAs(workStream);
            document.Close();

            byte[] byteInfo = workStream.ToArray();
            workStream.Write(byteInfo, 0, byteInfo.Length);
            workStream.Position = 0;

            return File(workStream, "application/pdf");
        }
    }

局部视图:

     function generarpdf(value) {

     $.ajax({
         url: '@Url.Action("PDFGenerator", "Home")',
         type: 'POST',
         async: false,
         data: { "id": value },
         dataType: "application/pdf",
         success: function (data) {
             var file = new Blob([data], { type: 'application/pdf' });
             var fileURL = URL.createObjectURL(file);
             window.open(fileURL);
         }
     });

};

被此 URL Action 调用。

    <a id="libro" href='@Url.Action("PDFGenerator","Home")'></a>

当我执行它时,控制台显示此错误:

jquery-3.2.1.min.js:4 [弃用] 主线程上的同步 XMLHttpRequest 已弃用,因为它对最终用户的体验产生不利影响。如需更多帮助,请查看https://xhr.spec.whatwg.org/

【问题讨论】:

  • 这是因为您在 ajax 调用中设置了 async: false。该警告告诉您,在未来版本的 jQuery 中将不支持这样做。您使用该设置是否有特殊原因?
  • 问题是最终 partial view 没有收到来自 ActionResult 的 File(workStream, "application/pdf") ,所以它没有显示上面有任何东西。

标签: c# ajax asp.net-mvc blob


【解决方案1】:

我终于做到了:

<a href='Home/PDFGenerator/'+  id+' "><img src="/Content/ImagesOwner/if_pdf_3745.png"/></a>

其中 id 是我需要发送到 HomeController 中的 ActionResult 的参数。我想最后很傻。

【讨论】:

    猜你喜欢
    • 2010-12-18
    • 2015-07-08
    • 1970-01-01
    • 2020-12-08
    • 1970-01-01
    • 1970-01-01
    • 2011-11-29
    • 2013-05-15
    相关资源
    最近更新 更多