public IHttpActionResult AddFile() { string result = ""; try { AngularDBEntities objEntity = new AngularDBEntities(); FileDetail objFile = new FileDetail(); string fileName = null; string imageName = null; var httpRequest = HttpContext.Current.Request; var postedImage = httpRequest.Files["ImageUpload"]; var postedFile = httpRequest.Files["FileUpload"]; objFile.UserName = httpRequest.Form["UserName"]; if (postedImage != null) { imageName = new String(Path.GetFileNameWithoutExtension(postedImage.FileName).Take(10).ToArray()).Replace(" ", "-"); imageName = imageName + DateTime.Now.ToString("yymmssfff") + Path.GetExtension(postedImage.FileName); var filePath = HttpContext.Current.Server.MapPath("~/Files/" + imageName); postedImage.SaveAs(filePath); } if (postedFile != null) { fileName = new String(Path.GetFileNameWithoutExtension(postedFile.FileName).Take(10).ToArray()).Replace(" ", "-"); fileName = fileName + DateTime.Now.ToString("yymmssfff") + Path.GetExtension(postedFile.FileName); var filePath = HttpContext.Current.Server.MapPath("~/Files/" + fileName); postedFile.SaveAs(filePath); } objFile.Image = imageName; objFile.DocFile = fileName; objEntity.FileDetails.Add(objFile); int i = objEntity.SaveChanges(); if (i > 0) { result = "File uploaded sucessfully"; } else { result = "File uploaded faild"; } } catch (Exception) { throw; } return(Ok(result)); }
public IHttpActionResult GetFile() { var url = HttpContext.Current.Request.Url; IEnumerable <FileDetailsVM> lstFile = new List <FileDetailsVM>(); try { AngularDBEntities objEntity = new AngularDBEntities(); lstFile = objEntity.FileDetails.Select(a => new FileDetailsVM { FileId = a.FileId, UserName = a.UserName, Image = url.Scheme + "://" + url.Host + ":" + url.Port + "/Files/" + a.Image, DocFile = a.DocFile, ImageName = a.Image }).ToList(); } catch (Exception) { throw; } return(Ok(lstFile)); }