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 !!
Chosen solutionOpen the Firefox Options window. Select the Applications tab. Locate Portable Document Format (PDF) on the list of file types. Change the Action to Preview In Firefox.
At the top right, click More Settings. At the bottom, click Show advanced settings. Under “Privacy”, click Content settings. Under “PDF Documents," check the box next to "Open PDF files in the default PDF viewer application.” (Uncheck this box if you want PDFs to open automatically when you click them.)
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With