2

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>

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

Here is pdf code

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

Thank you !!

marc_s
  • 675,133
  • 158
  • 1,253
  • 1,388
Ronak Dumaniya
  • 173
  • 1
  • 5
  • 14

1 Answers1

7

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

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) 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');
});

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.

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

Related issues:

How To Open PDF File In New Tab In MVC Using C#

ASP.NET MVC: How can I get the browser to open and display a PDF instead of displaying a download prompt?

Stream file using ASP.NET MVC FileContentResult in a browser with a name?

Tetsuya Yamamoto
  • 21,982
  • 5
  • 34
  • 53
  • your answer help me in another problem, I want to do label to open the pdf in another tab and I do : – Flowra Dec 10 '19 at 07:20