asp.net 如何使用Crystal Raport的内容生成HTML div并将其传递给javascript

tmb3ates  于 2023-02-01  发布在  .NET
关注(0)|答案(1)|浏览(86)

我正在创建一个带有crystal report的pdf文件,但我想将其转换为HTML元素,以便将其传递给Javascript函数并打印该元素。
下面是生成pdf文件的方法:

[HttpGet]
[AuthorizeUser()]
public ActionResult RaportFatureDTvsh(long FaturaID, int kursi = 1, int koefic = 1, string ekran_printer = "")
    {
        FatureDPaTvsh fdtvsh = new FatureDPaTvsh();
        string connstring = System.Configuration.ConfigurationManager.ConnectionStrings[GlobalVariables.DbName].ConnectionString;
        var builder = new EntityConnectionStringBuilder(connstring);
        var sqlconstring = builder.ProviderConnectionString;
        var sqlbuilder = new SqlConnectionStringBuilder(sqlconstring);
        fdtvsh.SetDatabaseLogon(sqlbuilder.UserID, sqlbuilder.Password, sqlbuilder.DataSource, sqlbuilder.InitialCatalog);
        ConnectionInfo cReportConnection = new ConnectionInfo();

        cReportConnection.DatabaseName = sqlbuilder.InitialCatalog;
        cReportConnection.ServerName = sqlbuilder.DataSource;
        cReportConnection.UserID = sqlbuilder.UserID;
        cReportConnection.IntegratedSecurity = false;
        cReportConnection.Password = sqlbuilder.Password;

        Tables tables = fdtvsh.Database.Tables;
        TableLogOnInfo crtablelogoninfo = new TableLogOnInfo();

        foreach (Table table in tables)
        {
            cReportConnection.DatabaseName = sqlbuilder.InitialCatalog;
            crtablelogoninfo.ConnectionInfo = cReportConnection;
            table.ApplyLogOnInfo(crtablelogoninfo);
            table.Location = table.Name;
        }
        fdtvsh.SetParameterValue(fdtvsh.Parameter_FaturaID.ParameterFieldName, FaturaID);
        fdtvsh.SetParameterValue(fdtvsh.Parameter_kursi.ParameterFieldName, kursi);
        fdtvsh.SetParameterValue(fdtvsh.Parameter_koefic.ParameterFieldName, koefic);

        FormulaFieldDefinition foField = fdtvsh.DataDefinition.FormulaFields["txtshenime"];
        string shenime = db.Ndermarrja.Select(x => x.shenime).FirstOrDefault();
        shenime = Regex.Replace(shenime, @"\r\n?|\n", "'+Chr(10)+ '");
        //PictureObject picObject = (PictureObject)fdtvsh.ReportDefinition.ReportObjects["Picture1"];

        foField.Text = string.Format("'{0}'", shenime);
        string picPath = db.Ndermarrja.Select(x => x.logopath).FirstOrDefault();
        if (!string.IsNullOrEmpty(picPath))
        {
            FormulaFieldDefinition picturePathFormula = fdtvsh.DataDefinition.FormulaFields["picturePath"];
            picturePathFormula.Text = string.Format("'{0}'", picPath);
        }

        if (ekran_printer == "Printer" || ekran_printer == "")
        {
            Stream s = fdtvsh.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);

            return File(s, "application/pdf", "FatureA4.pdf");
        }
        else
        {
            Stream s = fdtvsh.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);
            return File(s, "application/pdf");
        }
    }

我需要对这部分代码进行更改,使其不会创建pdf文件,而是将其内容放在HTML元素中。
我试着改变:

Stream s = fdtvsh.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);

        return File(s, "application/pdf", "FatureA4.pdf");

与:

Stream s = fdtvsh.ExportToStream(CrystalDecisions.Shared.ExportFormatType.HTML40);

        return File(s, "text/html", "FatureA4.html");

但是我得到了这个错误:

System.IO.FileNotFoundException
  HResult=0x80070002
  Message=Could not find file 'C:\Users\User\AppData\Local\Temp\cr_export_bae61988-381d-4496-a2d5-f3c281786bdb.htm'.
  Source=<Cannot evaluate the exception source>
  StackTrace:
<Cannot evaluate the exception stack trace>
unftdfkk

unftdfkk1#

您应该将pdf文件保存在服务器上,然后在它返回查看时,使用 AJAX 请求控制器中的ActionResult方法将其下载到客户端:

HttpResponse Response = System.Web.HttpContext.Current.Response;

Repsonse.ClearContent();
Repsonse.Clear();
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition", "attachment; filename=" + yourFileName + ";");
Repsonse.TransmitFile(your files full path);
Repsonse.Flush();
Repsonse.End();

相关问题