protected int UploadFile(HttpPostedFile file, UploadedReports U) { using (Stream fs = file.InputStream) { using (BinaryReader br = new BinaryReader(fs)) { byte[] bytes = br.ReadBytes((Int32)fs.Length); string constr = ConfigurationManager.ConnectionStrings["IqHealthConnection"].ConnectionString; using (MySqlConnection con = new MySqlConnection(constr)) { string query = "INSERT INTO `UploadedReports`(`UserID`,`FileName` ,`UploadedDate`,`UploadedBy` ,`CompanyID`,`File`) OUTPUT INSERTED.ID" + "VALUES (@CustomerID, @FileName, @UploadedDate, @UploadedBy, @CompanyID, @File)"; using (MySqlCommand cmd = new MySqlCommand(query)) { cmd.Connection = con; cmd.Parameters.AddWithValue("@CustomerID", U.UserID); cmd.Parameters.AddWithValue("@FileName", file.FileName); cmd.Parameters.AddWithValue("@FilePath", U.FilePath); cmd.Parameters.AddWithValue("@UploadedDate", DateTime.Now); cmd.Parameters.AddWithValue("@UploadedBy", U.UploadedBy); cmd.Parameters.AddWithValue("@CompanyID", U.CompanyID); cmd.Parameters.AddWithValue("@File", null); con.Open(); int id = (Int32)cmd.ExecuteNonQuery(); con.Close(); return(id); } } } } }
private async void SaveFileInFolder(HttpPostedFile file, UploadedReports U) { try { var filePath = U.UserID.ToString(); string fileName = U.UserID.ToString() + "_" + DateTime.Now.ToLongDateString(); string fileDirectory = AppUtil.GetUploadDirectory(AspectEnums.FileType.DiagnosticReport) + filePath; if (!Directory.Exists(fileDirectory)) { Directory.CreateDirectory(fileDirectory); } if (file != null) { file.SaveAs(fileDirectory + file.FileName); U.FilePath = filePath; U.FileName = file.FileName; _context.Entry(U).State = EntityState.Modified; await _context.SaveChangesAsync().ConfigureAwait(false); } } catch (Exception e) { //ActivityLog.SetLog("Image uploading failed! Message: " + e.Message + " Inner Ex: " + e.InnerException, LogLoc.INFO); } }
public JsonResponse <int> UploadCV() { JsonResponse <int> response = new JsonResponse <int>(); log.Info("[Started] Upload CV"); try { var httpRequest = HttpContext.Current.Request; if (httpRequest.Files.Count > 0) { int applicationID = Convert.ToInt32(httpRequest.Form["applicationID"]); int companyID = Convert.ToInt32(httpRequest.Form["companyID"]); log.Debug("Upload CV for ApplicationID: " + applicationID); foreach (string file in httpRequest.Files) { UploadedReports U = new UploadedReports(); var postedFile = httpRequest.Files[file]; var application = _context.JobApplications.Where(x => x.ID == applicationID && x.CompanyID == companyID).FirstOrDefault(); if (application != null) { StoreResume(postedFile, application); } } response.StatusCode = "200"; response.IsSuccess = true; } } catch (Exception ex) { response.IsSuccess = false; response.StatusCode = "500"; response.Message = ex.Message; log.Error("Error Message: " + ex.Message + " Inner Ex: " + ex.InnerException); log.Error("[Failed] Upload CV"); } log.Debug("[Finished] Upload CV"); return(response); }
public JsonResponse <int> UploadCustomerReport() { JsonResponse <int> response = new JsonResponse <int>(); try { var httpRequest = HttpContext.Current.Request; if (httpRequest.Files.Count > 0) { int EmpID = Convert.ToInt32(httpRequest.Headers["UserID"]); int UserType = Convert.ToInt32(httpRequest.Headers["UserType"]); int CompanyID = Convert.ToInt32(httpRequest.Headers["Company"]); int CustomerID = Convert.ToInt32(httpRequest.Form["customerID"]); foreach (string file in httpRequest.Files) { UploadedReports U = new UploadedReports(); var postedFile = httpRequest.Files[file]; U.UploadedBy = EmpID; U.UserID = CustomerID; U.FileName = postedFile.FileName; U.FilePath = postedFile.FileName; U.CompanyID = CompanyID; U.UploadedDate = DateTime.Now; U.FileType = (int)AspectEnums.FileType.DiagnosticReport; var fileExist = _context.UploadedReports.Where(x => x.FileName == U.FileName && x.CompanyID == U.CompanyID).FirstOrDefault(); if (fileExist != null) { response.StatusCode = "200"; response.IsSuccess = false; response.Message = "File with name " + U.FileName + " already exist. Rename and try again."; response.SingleResult = 0; return(response); } _context.UploadedReports.Add(U); _context.SaveChanges(); response.SingleResult = U.ID; SaveFileInFolder(postedFile, U); } response.StatusCode = "200"; response.IsSuccess = true; } else { response.StatusCode = "400"; response.IsSuccess = false; response.Message = "No any file to upload. Something went wrong. Contact Administrator."; } } catch (DataException ex) { response.SingleResult = 0; response.StatusCode = "500"; response.IsSuccess = false; response.Message = ex.InnerException.Message; } catch (Exception ex) { response.SingleResult = 0; response.StatusCode = "500"; response.IsSuccess = false; response.Message = ex.Message; } return(response); }