0
votes
function downloadpdForSalary() {
    $(".loader").show();
    $.ajax({
        type: 'POST',
        cache: false,
        data: $('#FormHeader').serialize(),
        url: "/SalarySheet/ExportSalarySheetTopdf",
        success: function (response) {
            $(".loader").hide();
            if (response.CommandStatus == "1") {
                window.location = '/Runreport/DownloadPdf?fileGuid=' + response.Width + '&filename=' + response.Height;
                showAndDismissAlert("success", "Downloaded Successfully");
            }
            else {
                showAndDismissAlert("error", response.CommandMessage);
            }
        },
        error: function (e) {
        }
    });
}

this is controllercode

[HttpPost] public ActionResult ExportSalarySheetTopdf(SalarySheetModel model) { var res = new JsonResponse(); try { if (model.BranchNameStr != null) model.BranchCode = string.Join(",", model.BranchNameStr); model.UserCode = UserCode; model.SessionId = SessionId; model.MenuCode = ResourceFile.LovResource.SalarySheet; var result = voucherSalaryBusiness.ExportSalarySheetToExcel(model); if (result.CommandStatus == "1") { DataTable dt = (DataTable)(JsonConvert.DeserializeObject(result.DataTableStr, (typeof(DataTable)))); XLWorkbook workbook = new XLWorkbook(); string handle = Guid.NewGuid().ToString(); workbook.Worksheets.Add(dt); using (MemoryStream memoryStream = new MemoryStream()) { workbook.SaveAs(memoryStream); memoryStream.Position = 0; TempData[handle] = memoryStream.ToArray(); } res.Width = handle; res.Height = "Salary Sheet.pdf"; res.PageName = "ExportToPdf";

            }

            res.CommandStatus = result.CommandStatus;
            res.CommandMessage = result.CommandMessage;

        }
        catch (Exception ex)
        {
            res.CommandStatus = "-1";
            res.CommandStatus = ex.Message;
        }
        var jsonResult = Json(res, JsonRequestBehavior.AllowGet);
        jsonResult.MaxJsonLength = int.MaxValue;
        return jsonResult;
    }

For Download PDf

[HttpGet] public virtual ActionResult DownloadPdf(string fileGuid, string fileName) { if (TempData[fileGuid] != null) { byte[] data = TempData[fileGuid] as byte[]; return File(data, "application/pdf", fileName); } else { return new EmptyResult(); } }

1
Please format the question properly and it seems you haven’t actually asked a question. Could you clarify?Sami Kuhmonen
Your poet is all code! Please add a clear question and format the code properly.MAW

1 Answers

0
votes

write following method in the controller

    public FileResult Download(string fileName)
    {
        string fullPath = Path.Combine(Server.MapPath("~/Files/"), fileName);
        //string fullPath = Server.MapPath("~/Files/") + fileName;
        byte[] fileBytes = System.IO.File.ReadAllBytes(fullPath);
        return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, fileName);
    }