我刚刚想出如何将我的RDL报告呈现为pdf文件,但它目前在临时文件夹中。下一步应该是在新窗口或标签中打开以供查看。我尝试了很多解决方案,但似乎没有任何工作。
这是我目前的代码:
[HttpGet]
[Route("printDCF")]
public IHttpActionResult printDCF(int controlFormDetailID)
{
try
{
ApiResult apiResult = new ApiResult();
var reportData = ControllerLogic.GetReportData(controlFormDetailID);
generateReport(reportData);
return Ok(apiResult);
}
catch (Exception ex)
{
return InternalServerError(ex);
}
}
public System.Web.Mvc.FileContentResult generateReport(List<DocumentControlFormPrintResult> reportData)
{
string RptPath = HttpContext.Current.Server.MapPath("~/AngularViews/forms/dcf/report/DCF_Report.rdl");
LocalReport rpt = new LocalReport();
rpt.DataSources.Add(new ReportDataSource("DataSet1", reportData));
rpt.ReportPath = RptPath;
string filePath = System.IO.Path.GetTempFileName();
Export(rpt, filePath);
rpt.Dispose();
System.Web.Mvc.FileContentResult result = new System.Web.Mvc.FileContentResult(System.IO.File.ReadAllBytes(filePath), "application/pdf")
{
FileDownloadName = "dcf_print.pdf",
};
return result;
}
public string Export(LocalReport rpt, string filePath)
{
string ack = "";
try
{
Warning[] warnings;
string[] streamids;
string mimeType;
string encoding;
string extension;
byte[] bytes = rpt.Render("PDF", null, out mimeType, out encoding, out extension, out streamids, out warnings);
using (FileStream stream = File.OpenWrite(filePath))
{
stream.Write(bytes, 0, bytes.Length);
}
return ack;
}
catch (Exception ex)
{
ack = ex.InnerException.Message;
return ack;
}
}
答案 0 :(得分:1)
我想,我试过这个代码对我来说很好,对你有用
[HttpGet]
[Route("api/Data/OpenPDF")]
public HttpResponseMessage OpenPDF()
{
var stream = new MemoryStream();
string filePath = @"D:\PDF\pdf-test.pdf";
using (FileStream fileStream = File.OpenRead(filePath))
{
stream = new MemoryStream();
stream.SetLength(fileStream.Length);
fileStream.Read(stream.GetBuffer(), 0, (int)fileStream.Length);
}
var result = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new ByteArrayContent(stream.ToArray())
};
result.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue(System.Net.Mime.DispositionTypeNames.Inline)
{
FileName = System.IO.Path.GetFileName(filePath)
};
result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
return result;
}