Advertise with us

How can Download uploaded files in MVC ?

Posted by  Sanat Shukla
 957  View(s)
Rate this:

Use Simple C# code for Download operation in MVC.

Don't use ajax script for downloads.

  1. Re: How can Download uploaded files in MVC ?

    4). Add a Download button (or Link button) in IndexView Page for download saved files. For this action, we can define only the ActionMethod for download. 

    ActionMethod for Download

    //---------------Download Files-------------//

            public ActionResult Downloads(int customerID)
                Customer model = AP.Customers.FirstOrDefault(x => x.CustomerID == customerID);

                if (model != null)
                    if (System.IO.File.Exists(Server.MapPath(resolveVirtual(model.UploadFiles))))
                        string fileName = Path.GetFileName(resolveVirtual(model.UploadFiles));
                        byte[] fileBytes = System.IO.File.ReadAllBytes(Server.MapPath(resolveVirtual(model.UploadFiles)).ToString());
                        return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, fileName);

                return null;

            public static string resolveVirtual(string physicalPath)
                string url = physicalPath.Substring(System.Web.HttpContext.Current.Server.MapPath("~").Length).Replace('\\', '/').Insert(0, "~/");
                return (url);

    5). Work on IndexViewPage.

    @using (Html.BeginForm("DownloadFile", "Controller", FormMethod.Get)){}

    How can Download uploaded files in MVC ?