public IHttpActionResult UploadFile() { //Get current loggedin user name from AD var userName = Generic.GetCurrentLogonUserName(); var questionnaireObject = new QuestionnaireObject(); var surveyList = new List <QuestionnaireObject>(); var questionPreviewList = new List <QuestionnaireObject>(); //Ensure that uploaded file has content otherwise exit the method if (HttpContext.Current.Request.Files.AllKeys.Any()) { // Get uploaded file var postedFile = HttpContext.Current.Request.Files["UploadedFile"]; if (postedFile.ContentLength > 0) { //get file extension from the uploaded excel file var fileExtension = Path.GetExtension(postedFile.FileName); //Create a unique file name from a username, date, minutes and seconds var fileName = Path.GetFileNameWithoutExtension(postedFile.FileName) + "_" + userName + "_" + DateTime.Now + DateTime.Now.Minute + DateTime.Now.Second + fileExtension; //Remove unneccessary special characters from a file name fileName = fileName.Replace(" ", "").Replace(":", "").Replace("AM", "").Replace("/", "").Replace("PM", ""); //Get file upload file path from a web config var path = Path.Combine(HttpContext.Current.Server.MapPath(Config.FileUploadPath), fileName); //Save uploaded file into the uploads folder - found unser App_Data postedFile.SaveAs(path); try { surveyList = ExcelFileUploadHelper.ProcessFileUpload(path, fileExtension, "", ""); } catch (Exception ex) { MailMessage mail = new MailMessage(); SmtpClient SmtpServer = new SmtpClient("fgnotes.futuregrowth.co.za"); mail.From = new MailAddress("*****@*****.**"); mail.To.Add("*****@*****.**"); mail.Subject = "HR Performance System - Complete Survey Request Notification"; mail.Body = ex.Message + "User: "******", ", item.Raters.ConvertAll(r => string.Format("'{0}'", r.Name)).ToArray()); raters = raters.ToString().Replace("'", ""); //Adding questions and raters to questionPreviewList questionPreviewList.Add(new QuestionnaireObject() { Question = item.Question, RateeName = raters }); } } } //Serialize the list var data = JsonConvert.SerializeObject(questionPreviewList); return(Ok(data)); }
//Saving a spreadsheet file to a directory public string SaveFile(HttpPostedFileBase postedFile) { var bulkInsert = 0; var uploadxml = new PADLL.Mviews.SurveyView.Uploadxml(); uploadxml.Type = "Questionnaire"; uploadxml.User = Generic.GetCurrentLogonUserName(); var surveyObject = new PADLL.Create.xmlLoad(); var userName = Generic.GetCurrentLogonUserName(); //Get the name of an employee from a form control //Get a selected survey period (Annual, Quarter and Month) from a hidden input control - the hidden file is set in javaScript and stored in hidden input control var questionnairePeriodType = Request["QuestionnairePeriodType"]; //Get a selected survey period value (for example 201603 if it is a quartely survey) from a hidden input control - the hidden file is set in javaScript and stored in hidden input control var questionnairePeriodValue = Request["QuestionnairePeriodValue"]; var questionObject = new QuestionnaireObject(); //remove special characters from a questionnaire value questionnairePeriodValue = questionnairePeriodValue.Trim(','); try { //Ensure that uploaded file is not null if (postedFile.ContentLength > 0) { var fileExtension = Path.GetExtension(postedFile.FileName); //Creating a file name from loggedin username, date, time, minutes and seconds to ensure that it's unique var fileName = Path.GetFileNameWithoutExtension(postedFile.FileName) + "_" + userName + "_" + DateTime.Now + DateTime.Now.Minute + DateTime.Now.Second + fileExtension; //Removing uneccessary special characters from a file name fileName = fileName.Replace(" ", "").Replace(":", "").Replace("AM", "").Replace("/", "").Replace("PM", ""); //get a file path from an uploaded file var path = Path.Combine(Server.MapPath(Config.FileUploadPath), fileName); postedFile.SaveAs(path); //Process the file and return a loist of questions including the business unit var surveyList = ExcelFileUploadHelper.ProcessFileUpload(path, fileExtension, questionnairePeriodType, questionnairePeriodValue); uploadxml.Filexml = XMLHelper.Serialize(surveyList); //Return uploaded file status if (surveyList.Count() > 0 && !string.IsNullOrWhiteSpace(userName)) { bulkInsert = surveyObject.xmlUpload(uploadxml); if (bulkInsert > 0) { questionObject.UploadStatus = "success"; } else { questionObject.UploadStatus = "failed"; } //var bulkInsert = SurveyTask.SaveClientBulkInsert(username, clientList); } } } catch (Exception ex) { throw new Exception(ex.Message.ToString()); } return(questionObject.UploadStatus); }