【问题标题】:Download PDF that I already Created ASP.Net下载我已经创建 ASP.Net 的 PDF
【发布时间】:2021-02-23 02:57:24
【问题描述】:

长话短说, 我想创建将 PDF 文件打印到用户计算机的按钮 因此,首先,我在服务器数据库/文件中创建 PDF 文件,然后将其下载给用户。

使用此代码,我可以在服务器中创建 PDF 文件, 然后我搜索了如何使其可下载给用户.. 到目前为止没有运气,大多数错误说“......是物理路径,但应该是虚拟路径。”

这是代码

private void cetak_pdf(string s_id, string CompanyCode , string zpath)
    {
        bool SUCCESS = true;
        string sErrMsg = "";
        string sFileName = "";

        DiskFileDestinationOptions diskOpts = null;

        try
        {
            sFileName = s_id;

            diskOpts = new DiskFileDestinationOptions();

            diskOpts.DiskFileName = Server.MapPath("~" + "\\pdf\\" + zpath + CompanyCode + "_" + sFileName + ".pdf");

            //Response.Write(diskOpts.DiskFileName);Response.End();
            // this is the file created E:\GIA_25\pdf\CompanyAddress00000003_003.pdf

            rptDoc.ExportOptions.ExportFormatType = ExportFormatType.PortableDocFormat;
            rptDoc.ExportOptions.ExportDestinationType = ExportDestinationType.DiskFile;
            rptDoc.ExportOptions.DestinationOptions = diskOpts;

            rptDoc.Export();


            Response.ContentType = "application/pdf";
            //Response.AppendHeader("Content-Disposition", "attachment; filename=MyFile.pdf");
            Response.TransmitFile(Server.MapPath("E:\\GIA_25\\pdf\\CompanyAddress00000003_003.pdf"));
            Response.End();


        }
        catch (Exception ex)
        {
            SUCCESS = false;
            sErrMsg = ex.Message;
            throw new Exception(sErrMsg);
        }
    }

请帮忙,这门语言有点新

【问题讨论】:

    标签: asp.net asp.net-core pdf


    【解决方案1】:

    对不起我的错 :(这是使它可下载的正确代码

    Response.ContentType = "APPLICATION/OCTET-STREAM";
                    // String Header = "Attachment; Filename=XMLFile.xml";
                    String Header = "Attachment; Filename=" + "CompanyAddress" + CompanyCode + "_" + sFileName + ".pdf";
                    Response.AppendHeader("Content-Disposition", Header);
                    System.IO.FileInfo Dfile = new System.IO.FileInfo(Server.MapPath("~" + "\\pdf\\" + "CompanyAddress" + CompanyCode + "_" + sFileName + ".pdf"));
                    Response.WriteFile(Dfile.FullName);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多