How to upload Excel and PDF file using JQuery Ajax MVC

I am using MVC application. I want to upload Excel file and PDF file using JQuery AJAX.

On the view page

 <a href="javascript:void(0)" class="excelbtn" data-is-pdf="false" >Export To Excel</a>
 <a href="javascript:void(0)" class="pdfbtn" data-is-pdf="true">Export To PDF</a>

      

Jquery ajax

$.ajax({
        type: 'GET',
        url: '/Report/ExportReports',
        contentType:"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
        data: {
            Parameter1: Parameter1,
            Parameter2: Parameter2,
        },
        cache: false,
        success: function (isSuccess) {
            if (isSuccess.Success) {
                }
            } else {
                alert('Something went wrong. Please try again after sometime...');
            }
        },
        error: function (data, status, e) {
        }
    });

      

In the controller

public ActionResult ExportReports(string Parameter1, string Parameter2)
    {
       if (Parameter1 = "PDF")
        {
            DataTable exportData = grid.GetExportData(dataSource);
            MemoryStream pdfStream = gridData.ExportToPDF(exportData, repType);

            Response.ClearContent();
            Response.ClearHeaders();
            Response.ContentType = "application/pdf";
            Response.AppendHeader("Content-Disposition", "attachment; filename=" + executeRepType + ".pdf");
            Response.BinaryWrite(pdfStream.ToArray());
            Response.End();
        }
        else
        {
            DataTable exportData = grid.GetExportData(dataSource);
            MemoryStream excelStream = gridData.ExportToExcel(exportData, executeRepType);
            //Write it back to the client
            Response.ClearContent();
            Response.ClearHeaders();
            Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
            Response.AddHeader("content-disposition", "attachment;  filename=" + executeRepType + ".xlsx");
            Response.BinaryWrite(excelStream.ToArray());//.GetAsByteArray());
            Response.End();
        }
        return View();
    }

      

So, in the controller, we get all the data, but we cannot return to the view page.

+3


source to share


2 answers


You can try this solution. On the view page

@Html.ActionLink("Export To Excel", "ExportReports", new { isPdfExport = false,Parameter1="_Parameter1" ,Parameter2="_Parameter2"}, new { @class="excelbtn" })

@Html.ActionLink("Export To PDF", "ExportReports", new { isPdfExport = true,Parameter1="_Parameter1" ,Parameter2="_Parameter2"}, new { @class="pdfbtn" })

      

if you want to dynamically change the value of parameter1 and parameter2 than you can use javascript as explained below

In Javascript: -



$('.excelbtn').attr('href', function () {
        return this.href.replace('_Parameter1', Value1).replace('_Parameter2',Value2);
          });
$('.pdfbtn').attr('href', function () {
        return this.href.replace('_Parameter1', Value1).replace('_Parameter2',Value2);
          });

      

In the controller: -

public ActionResult ExportReports(bool isPdfExport,string Parameter1, string Parameter2)
{
   if (Parameter1 = "PDF")
    {
        DataTable exportData = grid.GetExportData(dataSource);
        MemoryStream pdfStream = gridData.ExportToPDF(exportData, repType);

        Response.ClearContent();
        Response.ClearHeaders();
        Response.ContentType = "application/pdf";
        Response.AppendHeader("Content-Disposition", "attachment; filename=" + executeRepType + ".pdf");
        Response.BinaryWrite(pdfStream.ToArray());
        Response.End();
    }
    else
    {
        DataTable exportData = grid.GetExportData(dataSource);
        MemoryStream excelStream = gridData.ExportToExcel(exportData, executeRepType);
        //Write it back to the client
        Response.ClearContent();
        Response.ClearHeaders();
        Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
        Response.AddHeader("content-disposition", "attachment;  filename=" + executeRepType + ".xlsx");
        Response.BinaryWrite(excelStream.ToArray());//.GetAsByteArray());
        Response.End();
    }
    return View();
}

      

+3


source


I suggest you make things a little easier with the tools that come out of the box. System.Web.MVC.Controller.File provides you with a method that will do exactly what you need using a byte array or stream or file path. So instead of this part (and same for pdf)

Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("content-disposition", "attachment;  filename=" + executeRepType + ".xlsx");
Response.BinaryWrite(excelStream.ToArray());//.GetAsByteArray());
Response.End();

      

I would use something like this



File(excelStream.ToArray(), System.Net.Mime.MediaTypeNames.Application.Octet, executeRepType + ".xlsx");

      

And there is no need for an asynchronous request. Therefore, you can simply use the direct link.

0


source







All Articles