I do AJAX call to generate PDF from crystal report.
我做AJAX调用从水晶报告生成PDF。
Problem is , how to open PDF directly.
问题是,如何直接打开PDF。
Below is my stuff. It looks, pdf is created but, it just return to view and not able to open PDF.
以下是我的东西。看起来,pdf已创建,但它只是返回查看而无法打开PDF。
Please guide me.
请指导我。
Code:
public ActionResult CreatePDF(string paramValue)
{
DataTable dt = new DataTable();
DataColumn dc = new DataColumn("FieldName");
dt.Columns.Add(dcinitial);
DataRow dr = dt.NewRow();
dr[0]=paramValue;
dt.Rows.Add(dr);
ReportDocument oRpt = new ReportDocument();
string path = Server.MapPath("~/PDFDocs/crystalreport.rpt");
oRpt.Load(path);
oRpt.SetDataSource(dt);
MemoryStream oStream = new MemoryStream();
oStream = (MemoryStream)oRpt.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);
Response.Clear();
Response.Buffer = true;
Response.ContentType = "application/pdf";
string fileName = "Report";
Response.AppendHeader("content-disposition", "attachment; filename=" + fileName);
Response.BinaryWrite(oStream.ToArray());
Response.End();
return View();
}
Thanks
1 个解决方案
#1
0
Don't write to the Response
in the controller. Aside from coupling issues and testability, it's confusing the functionality of that controller action. This code is trying to both write the file to the response and return the view.
不要写入控制器中的响应。除了耦合问题和可测试性之外,它还混淆了该控制器操作的功能。此代码尝试将文件写入响应并返回视图。
Instead, just return the file, which you can do directly from the stream:
相反,只需返回文件,您可以直接从流中执行此操作:
return File(oStream, "application/pdf", fileName);
However...
I do AJAX call
我做AJAX通话
That's going to complicate things a bit. Ideally this wouldn't be an AJAX call. This is because AJAX doesn't handle "files" in a way you might think. There are options, but it would probably be easier to make this a normal page-level request so the browser can handle the file natively.
这会让事情变得复杂一些。理想情况下,这不是AJAX调用。这是因为AJAX不会以您可能想到的方式处理“文件”。有选项,但可能更容易使这成为正常的页面级请求,因此浏览器可以本机处理该文件。
Since it's returning a file and not a view, the browser won't unload the current page. (Unless it's configured to display the file instead of save it, though there isn't much you can do about that. The server-side code is properly suggesting that the browser save the file by providing a Content-Disposition
header.)
由于它返回的是文件而不是视图,因此浏览器不会卸载当前页面。 (除非它被配置为显示文件而不是保存它,但是你可以做的很少。服务器端代码正确地建议浏览器通过提供Content-Disposition标头来保存文件。)
#1
0
Don't write to the Response
in the controller. Aside from coupling issues and testability, it's confusing the functionality of that controller action. This code is trying to both write the file to the response and return the view.
不要写入控制器中的响应。除了耦合问题和可测试性之外,它还混淆了该控制器操作的功能。此代码尝试将文件写入响应并返回视图。
Instead, just return the file, which you can do directly from the stream:
相反,只需返回文件,您可以直接从流中执行此操作:
return File(oStream, "application/pdf", fileName);
However...
I do AJAX call
我做AJAX通话
That's going to complicate things a bit. Ideally this wouldn't be an AJAX call. This is because AJAX doesn't handle "files" in a way you might think. There are options, but it would probably be easier to make this a normal page-level request so the browser can handle the file natively.
这会让事情变得复杂一些。理想情况下,这不是AJAX调用。这是因为AJAX不会以您可能想到的方式处理“文件”。有选项,但可能更容易使这成为正常的页面级请求,因此浏览器可以本机处理该文件。
Since it's returning a file and not a view, the browser won't unload the current page. (Unless it's configured to display the file instead of save it, though there isn't much you can do about that. The server-side code is properly suggesting that the browser save the file by providing a Content-Disposition
header.)
由于它返回的是文件而不是视图,因此浏览器不会卸载当前页面。 (除非它被配置为显示文件而不是保存它,但是你可以做的很少。服务器端代码正确地建议浏览器通过提供Content-Disposition标头来保存文件。)