Ron*_*iya 3 c# pdf asp.net-mvc
我有发票屏幕,在此屏幕中有可用的订单数量,因此当我们创建发票时,我们需要填写一张表格,所以我想要的解决方案是当我提交此发票表格或单击此提交按钮时 pdf 应以新格式打开标签。我想向您澄清,我们不会将此 pdf 保存在任何地方。
<div class="modal-footer custom-no-top-border">
<input type="submit" class="btn btn-primary" id="createdata" value="@T("Admin.Common.Create")" />
</div>
Run Code Online (Sandbox Code Playgroud)
当我单击此按钮时,pdf 应在新选项卡中打开。
这是pdf代码
[HttpPost]
public virtual ActionResult PdfInvoice(int customerOrderselectedId)
{
var customerOrder = _customerOrderService.GetCustomerOrderById(customerOrderselectedId);
var customerOrders = new List<DD_CustomerOrder>();
customerOrders.Add(customerOrder);
byte[] bytes;
using (var stream = new MemoryStream())
{
_customerOrderPdfService.PrintInvoicePdf(stream, customerOrders);
bytes = stream.ToArray();
}
return File(bytes, MimeTypes.ApplicationPdf, string.Format("order_{0}.pdf", customerOrder.Id));
}
Run Code Online (Sandbox Code Playgroud)
当我单击按钮时,此代码会下载 pdf。
谢谢 !!
最重要的是Controller.File()与 一起使用[HttpGet],因此您应该执行以下步骤:
1) 将 HTTP 方法类型从[HttpPost]to[HttpGet]和 set更改为return File()不指定fileDownloadName参数(使用Controller.File()接受 2 个参数的重载)。
[HttpGet]
public virtual ActionResult PdfInvoice(int customerOrderselectedId)
{
var customerOrder = _customerOrderService.GetCustomerOrderById(customerOrderselectedId);
var customerOrders = new List<DD_CustomerOrder>();
customerOrders.Add(customerOrder);
byte[] bytes;
using (var stream = new MemoryStream())
{
_customerOrderPdfService.PrintInvoicePdf(stream, customerOrders);
bytes = stream.ToArray();
}
// use 2 parameters
return File(bytes, MimeTypes.ApplicationPdf);
}
Run Code Online (Sandbox Code Playgroud)
2) 处理click该按钮的事件(首选使用<input type="button" .../>)和使用_blank选项,或使用<a>带有target='_blank'属性的锚标记 ( ) :
$('#createdata').click(function (e) {
// if using type="submit", this is mandatory
e.preventDefault();
window.open('@Url.Action("PdfInvoice", "ControllerName", new { customerOrderselectedId = selectedId })', '_blank');
});
Run Code Online (Sandbox Code Playgroud)
fileDownloadName此处不使用参数的原因是参数设置Content-Disposition: attachment时提供文件名,否则如果省略或使用null值,则将Content-Disposition: inline自动设置。
需要注意的是,因为你使用FileResult,你不应该设置Content-Disposition使用Response.AddHeader前return File()这个样子,因为这样做会发送多个Content-Disposition它导致浏览器不显示文件头:
// this is wrong way, should not be used
Response.AddHeader("Content-Disposition", "inline; filename=order_XXX.pdf");
return File(bytes, MimeTypes.ApplicationPdf);
Run Code Online (Sandbox Code Playgroud)
相关问题:
ASP.NET MVC:如何让浏览器打开并显示 PDF 而不是显示下载提示?
在带有名称的浏览器中使用 ASP.NET MVC FileContentResult 流式传输文件?
| 归档时间: |
|
| 查看次数: |
11225 次 |
| 最近记录: |