[英]How to open PDF file in a new tab or window instead of downloading it using C# and ASP.NET MVC?
I have invoice screen and in this screen there are number of order are available so when we create invoice there are one form we need to fill so I want solution is when I submit this invoice form or click this submit button pdf should be open in new tab. 我有发票屏幕,在此屏幕上有很多订单可用,所以当我们创建发票时,我们需要填写一张表格,所以我想解决的方法是当我提交此发票表格或单击此提交按钮时,pdf应该在新窗口中打开标签。 I want to clarify to you we are not save this pdf anywhere.
我想向您澄清,我们不会在任何地方保存该pdf文件。
<div class="modal-footer custom-no-top-border">
<input type="submit" class="btn btn-primary" id="createdata" value="@T("Admin.Common.Create")" />
</div>
When I click on this button, the pdf should be opened in a new tab. 当我单击此按钮时,应该在新选项卡中打开pdf。
Here is pdf code 这是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));
}
This code downloads the pdf when I click on the button. 当我单击按钮时,此代码下载pdf。
Thank you !! 谢谢 !!
The most important thing is Controller.File()
works with [HttpGet]
, hence you should do these steps: 最重要的是
Controller.File()
与[HttpGet]
,因此您应该执行以下步骤:
1) Change HTTP method type from [HttpPost]
to [HttpGet]
and set return File()
without specifying fileDownloadName
parameter (using overload of Controller.File()
which accepts 2 parameters). 1)将HTTP方法类型从
[HttpPost]
更改为[HttpGet]
并设置return File()
而不指定fileDownloadName
参数(使用接受2个参数的Controller.File()
重载)。
[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);
}
2) Handle click
event of that button (preferred using <input type="button" .../>
) and use _blank
option, or use an anchor tag ( <a>
) with target='_blank'
attribute: 2)处理该按钮的
click
事件(优选使用<input type="button" .../>
)并使用_blank
选项,或者使用具有target='_blank'
属性的锚标记( <a>
):
$('#createdata').click(function (e) {
// if using type="submit", this is mandatory
e.preventDefault();
window.open('@Url.Action("PdfInvoice", "ControllerName", new { customerOrderselectedId = selectedId })', '_blank');
});
The reason why fileDownloadName
parameter is not used here is that parameter sets Content-Disposition: attachment
while file name is provided, otherwise if you're omit it or using null
value, then Content-Disposition: inline
will be set automatically. 之所以不使用
fileDownloadName
参数,是因为该参数在提供文件名时设置了Content-Disposition: attachment
,否则,如果您省略它或使用null
值,则将自动设置Content-Disposition: inline
。
Note that because you're using FileResult
, you should not setting Content-Disposition
using Response.AddHeader
before return File()
like this, because doing so will sent multiple Content-Disposition
headers which causing browser to not display the file: 请注意,由于使用的是
FileResult
,因此不应在return File()
之前使用Response.AddHeader
设置Content-Disposition
,因为这样做会发送多个Content-Disposition
标头,从而导致浏览器无法显示文件:
// this is wrong way, should not be used
Response.AddHeader("Content-Disposition", "inline; filename=order_XXX.pdf");
return File(bytes, MimeTypes.ApplicationPdf);
Related issues: 相关问题:
How To Open PDF File In New Tab In MVC Using C# 如何使用C#在MVC的新选项卡中打开PDF文件
ASP.NET MVC: How can I get the browser to open and display a PDF instead of displaying a download prompt? ASP.NET MVC:如何使浏览器打开并显示PDF,而不显示下载提示?
Stream file using ASP.NET MVC FileContentResult in a browser with a name? 在具有名称的浏览器中使用ASP.NET MVC FileContentResult流文件?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.