如何在新的标签页或窗口中打开PDF文件,而不是使用C#和ASP.NET MVC下载它? [英] How to open PDF file in a new tab or window instead of downloading it using C# and ASP.NET MVC?

查看:47
本文介绍了如何在新的标签页或窗口中打开PDF文件,而不是使用C#和ASP.NET MVC下载它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有发票屏幕,并且在此屏幕上有可用的订单数量,因此当我们创建发票时,我们需要填写一张表格,因此我想解决的方法是当我提交此发票表格或单击此提交按钮时pdf应该是在新标签页中打开.我想向您澄清,我们不会将此pdf文件保存在任何地方.

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. I want to clarify to you we are not save this pdf anywhere.

<div class="modal-footer custom-no-top-border">
      <input type="submit" class="btn btn-primary" id="createdata" value="@T("Admin.Common.Create")" />
</div>

当我单击此按钮时,应在新标签页中打开pdf.

When I click on this button, the pdf should be opened in a new tab.

这是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));
    }

当我单击按钮时,此代码将下载pdf.

This code downloads the pdf when I click on the button.

谢谢!!

推荐答案

最重要的是 Controller.File() [HttpGet] 一起使用,因此您应该请执行以下步骤:

The most important thing is Controller.File() works with [HttpGet], hence you should do these steps:

1)将HTTP方法类型从 [HttpPost] 更改为 [HttpGet] ,并设置 return File(),而无需指定 fileDownloadName参数(使用

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).

[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)处理该按钮的 click 事件(最好使用< input type ="button" .../> )并使用 _blank 选项,或将锚标记(< a> )与 target ='_ blank'属性一起使用:

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:

$('#createdata').click(function (e) {
    // if using type="submit", this is mandatory
    e.preventDefault();

    window.open('@Url.Action("PdfInvoice", "ControllerName", new { customerOrderselectedId = selectedId })', '_blank');
});

此处未使用 fileDownloadName 参数的原因是,该参数在提供文件名时设置了 Content-Disposition:附件,否则,请省略或使用 null 值,然后将自动设置 Content-Disposition:inline .

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.

请注意,因为您使用的是 FileResult ,所以您不应在返回文件之前使用 Response.AddHeader 设置 Content-Disposition (),因为这样做会发送多个 Content-Disposition 标头,这些标头会导致浏览器无法显示该文件:

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:

// this is wrong way, should not be used
Response.AddHeader("Content-Disposition", "inline; filename=order_XXX.pdf");
return File(bytes, MimeTypes.ApplicationPdf);

相关问题:

如何使用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 FileContentResult命名文件?

这篇关于如何在新的标签页或窗口中打开PDF文件,而不是使用C#和ASP.NET MVC下载它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆