I can generate and download a PDF file successfully if there's only one. However, when I attempt to generate and download multiple PDFs, I encounter an issue. I'm using the same code that works for downloading a single PDF. Instead of downloading the PDFs, they are only stored in the designated location.
Thank you in advance for the help.
Here is my js:
function GeneratePackingSlipAction() {
var checkedVals = GetSelectedOrders();
if (checkedVals.length > 0) {
$.ajax({
url: '/Orders/PackingSlipBulkOrders',
type: 'POST',
data: { selectedOrderIds: checkedVals }
}).done(function (data) {
console.log(data);
alert("was heree:" + data.status);
if (data.status == true) {
var msg = "Generated Packing Slip " + data.successCount + " order(s)";
alert(msg);
jQuery(this).removeClass("checkedall");
jQuery(".SelectOrder").prop("checked", false);
}
});
}
}
And the c# controller code:
public ActionResult PackingSlipBulkOrders(long[] selectedOrderIds)
{
int successCount = 0;
int failedCount = 0;
if (selectedOrderIds.Count() > 0)
{
foreach (var id in selectedOrderIds)
{
try
{
Order order = _orderMgr.GetOrder(id);
var result = GenerateExcelPDFBulk(order);
successCount++;
}
catch
{
failedCount++;
}
}
}
return Json(new { status = true, successCount = successCount, failedCount = failedCount });
}
public bool GenerateExcelPDFBulk(Order order)
{
PDFHelperPackingSlip _pdfHelper = new PDFHelperPackingSlip();
string footerimgpath = Server.MapPath("~/Content/images/dark-logo.png");
//Create Excel File and Save
string FileName = "PackingSlip.pdf";
string fileLocation = Server.MapPath("~/Content/Imports/") + FileName;
if (System.IO.File.Exists(fileLocation))
{
System.IO.File.Delete(fileLocation);
}
string filename = _pdfHelper.GeneratePDF(order, fileLocation, footerimgpath);
Response.ContentType = "application/pdf";
Response.AppendHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(fileLocation));
Response.TransmitFile(fileLocation);
Response.Flush();
Response.Close();
Response.End();
return true;
}