Reputation: 1065
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 !!
Upvotes: 9
Views: 27757
Reputation: 24957
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#
Stream file using ASP.NET MVC FileContentResult in a browser with a name?
Upvotes: 22