简体   繁体   English

如何在新选项卡或窗口中打开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?

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.

相关问题 如何在新的标签页或窗口中打开PDF文件而不是下载文件(使用asp.net)? - How to open PDF file in a new tab or window instead of downloading it (using asp.net)? asp.net:如何使用c#在新窗口(不是标签页或当前页面)中打开新的asp.net页 - asp.net: How to open a new asp.net page in new window (not tab or current page) using c# 如何在ASP.NET C#中使用GridView行命令在浏览器的新选项卡中打开PDF文件 - How To Open PDF Files In New Tab In Browser Using GridView Row Command In ASP.NET C# 如何使用asp.net C#在Chrome的新标签页中打开打开的PDF - How to itextsharp PDF open in new tab in chrome using asp.net c# 如何在新标签Asp.net中打开pdf文件 - How to open pdf file in new tab Asp.net 使用 ASP.NET 按钮、C# 和页面背后的代码在新选项卡中打开窗口 - Open Window in new tab using an ASP.NET Button, C#, and the code behind the page 如何在ASP.NET C#中的新选项卡(NOT WINDOW)中打开页面? - How to open page in new Tab (NOT WINDOW) in ASP.NET C#? 如何在 ASP.NET MVC C# 项目的新选项卡中打开 PowerPoint 演示文稿 - How to open PowerPoint Presentation in New Tab in ASP.NET MVC C# project MVC.Net在新窗口中打开文件,而不是下载 - MVC.Net Open a file in a new window instead of downloading ASP.NET MVC - 在新标签上有条件地打开PDF /图像 - ASP.NET MVC - Conditionally open PDF/Image on new Tab
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM