public Object DownloadFile(String uniqueName) { HttpContext.Current.Response.AppendHeader("Access-Control-Allow-Origin", "*"); //Physical Path of Root Folder string rootPath = System.Web.HttpContext.Current.Server.MapPath("~/UploadedFiles"); FileModel.GetFilesInPath(rootPath); //Find File from DB against unique name FileDTO fileDTO = FileModel.GetFileByUniqueID(uniqueName); if (fileDTO != null) { HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK); var fileFullPath = System.IO.Path.Combine(rootPath, fileDTO.FileUniqueName + fileDTO.FileExt); byte[] file = System.IO.File.ReadAllBytes(fileFullPath); System.IO.MemoryStream ms = new System.IO.MemoryStream(file); response.Content = new ByteArrayContent(file); response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment"); //String mimeType = MimeType.GetMimeType(file); //You may do your hard coding here based on file extension response.Content.Headers.ContentType = new MediaTypeHeaderValue(fileDTO.ContentType);// obj.DocumentName.Substring(obj.DocumentName.LastIndexOf(".") + 1, 3); response.Content.Headers.ContentDisposition.FileName = fileDTO.FileActualName + fileDTO.FileExt; return(response); } else { HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.NotFound); return(response); } }
{ public ActionResult Index() { //Physical Path of Root Folder string rootPath = System.Web.HttpContext.Current.Server.MapPath("~/UploadedFiles"); // List <FileDTO> files = new List <FileDTO>(); if (Directory.Exists(rootPath)) { //var files = FileModel.GetAllFiles(); files = FileModel.GetFilesInPath(rootPath); } else { } return(View(files)); }