首页 > 解决方案 > 下载我已经创建 ASP.Net 的 PDF

问题描述

长话短说,我想创建将 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.netasp.net-corepdf

解决方案


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

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);

推荐阅读