download excel file using c# mvc 5 jquery

download excel file using c# mvc 5 jquery

We can download files using the below mvc 5 code or jQuery code both code are woking. C# code work in server side but jQuery on client only.

Download Excel File using C# MVC 5 .Net

public ActionResult DownloadExcelFile(string Orgfile = "DownloadSampleFile.xlsx")
{
     string filePath = Server.MapPath("~/UploadedExcelFolder/DownloadSampleFile.xlsx");
     System.IO.FileInfo file = new System.IO.FileInfo(filePath);

     if (file.Exists)
     {
          Response.Clear();
          Response.AddHeader("Content-Disposition", "attachment; filename=" + Orgfile);
          Response.AddHeader("Content-Length", file.Length.ToString());
          Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
          Response.WriteFile(file.FullName);
       }
       return RedirectToAction("Success");
}

OR 

 Download Excel File using jQuery

$('#btnDownloadExcel').click(function () {
        window.location.href = "UploadedExcelDocuments/DownloadSampleFile.xlsx";
});
ANIL SINGH

Anil Singh is an author, tech blogger, and software programmer. Book writing, tech blogging is something do extra and Anil love doing it. For more detail, kindly refer to this link..
My Blogs - https://code-sample.com and https://code-sample.xyz
My Book1 - BEST SELLING ANGULAR BOOK (INCLUDING ALL VERSIONS 2, 4, 5, 6, 7)
My Book2 - ANGULAR 2 INTERVIEW QUESTIONS BOOK - Both Books are Available on WorldWide.

www.code-sample.com/. Powered by Blogger.