public HttpResponseMessage UploadLogo() { try { if (!Request.Content.IsMimeMultipartContent()) { throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType); } var CompanyLogo = HttpContext.Current.Request.Files["CompanyLogo"]; var CompanyId = Convert.ToInt32(HttpContext.Current.Request.Form["CompanyId"]); string path = HttpContext.Current.Server.MapPath("~/UploadedDocuments"); bool folderExists = Directory.Exists(path); if (!folderExists) { Directory.CreateDirectory(path); } var fileName = DateTime.Now.Ticks + Path.GetExtension(CompanyLogo.FileName); var fileSavePath = Path.Combine(path, fileName); CompanyLogo.SaveAs(fileSavePath); var cmpData = new AddLogo(); if (File.Exists(fileSavePath)) { var cmp = _companyServices.GetCompanyById(CompanyId); var fileToDelete = Path.Combine(path, cmp.CompanyLogo); File.Delete(fileToDelete); cmpData.CompanyId = CompanyId; cmpData.CompanyLogo = fileName; } var result = _companyServices.CompanyLogoUpload(cmpData); return(Request.CreateResponse(HttpStatusCode.OK, "success!")); } catch (Exception ex) { throw new ApiDataException(1000, "Company Not Found", HttpStatusCode.NotFound); } }