public ActionResult FileImport(VMUpload form) { if (ModelState.IsValid) { if (form != null && form.File != null && form.File.ContentLength > 0) { var tempPath = string.Concat(HostingEnvironment.MapPath("~/temp/" + Guid.NewGuid().ToString()), Path.GetExtension(form.File.FileName)).ToLower(); form.File.SaveAs(tempPath); var threadData = new Tuple <string, string, string, VMUpload>(tempPath, Company, LoggedUserID, form); ThreadPool.QueueUserWorkItem((o) => { var input = o as Tuple <string, string, string, VMUpload>; if (input != null) { switch (input.Item4.UploadType) { case "S": Web.Service.CommonService.Instance.ImportStudents(input.Item1, input.Item2, input.Item3); break; case "Q": var quiz = _db.GetQuiz(input.Item4.QuizId); if (quiz != null) { var dbFilePath = Path.Combine(CommonService.GetSQLiteDbFileDirectoryBasePath(), quiz.QuestionDbFile); Web.Service.CommonService.Instance.ImportQuestions(input.Item1, dbFilePath, input.Item3, quiz.ID); } break; default: break; } } }, threadData); } } return(View()); }
public ActionResult Upload(int?id, string path = "") { if (!id.HasValue) { return(RedirectToAction("Index")); } FTPK_FTPs site = dbContext.FTPK_FTPs.Where(x => x.ID == id.Value).FirstOrDefault(); if (site == null) { return(HttpNotFound("No FTP Site with ID '" + id.Value + "' found")); } if (!SecurityManager.canUpload(site.ID)) { return(new HttpStatusCodeResult(HttpStatusCode.Unauthorized)); } VMUpload model = new VMUpload(id.Value, path, site.DisplayName); return(View(model)); }
public ActionResult Upload(VMUpload model) { if (model == null) { return(HttpNotFound()); } if (model.path == null) { model.path = ""; } AppResponse <List <string> > resp = uploadFiles(model.files); if (!resp.success) { ModelState.AddModelError("files", resp.message); return(View(model)); } List <string> filesToUpload = resp.getData(); FTPK_FTPs site = dbContext.FTPK_FTPs.Where(x => x.ID == model.id).FirstOrDefault(); if (site == null) { return(HttpNotFound()); } if (!SecurityManager.canUpload(site.ID)) { return(new HttpStatusCodeResult(HttpStatusCode.Unauthorized)); } SftpClientWrapper sftp = initSFTP(site); string fullPath = Server.MapPath(localFileDir); DirectoryInfo localDir = new DirectoryInfo(fullPath); int failedCnt = 0; foreach (string fName in filesToUpload) { try { FTPK_Logs log = new FTPK_Logs(); log.SiteID = model.id; log.Action = ACTION_UPLOAD; log.FileName = fName; log.Path = model.path; log.UserID = SecurityManager.getUserID(); log.LogDate = DateTime.Now; dbContext.FTPK_Logs.Add(log); dbContext.SaveChanges(); resp.SetSuccess(); } catch (Exception ex) { resp.SetFailure(ex.Message); } bool success = sftp.uploadFile(model.path, fName, localDir); if (!success) { ModelState.AddModelError("files", fName + " failed to upload. Please retry this file."); failedCnt++; } } AppResponse <Object> mResp = new AppResponse <object>(); if (!ModelState.IsValid) { mResp.SetFailure(failedCnt + " of " + filesToUpload.Count() + " file(s) uploaded successfully!"); } else { mResp.SetSuccess(); } model.files = null; model.response = mResp; return(View(model)); }