public async Task <IActionResult> Edit(int id, [Bind("ID,userName")] fileUpload fileUpload) { if (id != fileUpload.ID) { return(NotFound()); } if (ModelState.IsValid) { try { _context.Update(fileUpload); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!fileUploadExists(fileUpload.ID)) { return(NotFound()); } else { throw; } } return(RedirectToAction("Index")); } return(View(fileUpload)); }
public async Task <IActionResult> Create([Bind("ID,userName")] fileUpload fileUpload) { if (ModelState.IsValid) { _context.Add(fileUpload); await _context.SaveChangesAsync(); return(RedirectToAction("Index")); } return(View(fileUpload)); }
public async Task <IActionResult> CreateUploadButton([Bind("ID,userName,caseName,unitModel")] fileUpload fileUpload, ICollection <IFormFile> uploadedFile) { //Checking for any errors var errors = ModelState.Where(x => x.Value.Errors.Count > 0).Select(x => new { x.Key, x.Value.Errors }).ToArray(); if (ModelState.IsValid) { if (uploadedFile.Count == 0) { // Save errore message to "ViewData", dictionary whico offers one way to pass data from controller to View. ViewData["file_errormessage"] = "Please select file!"; return(View("Create")); // WORKS: //return RedirectToAction("Create", "fileUploads"); //looses VIEWDATA } else if (uploadedFile.First().Length > maxFileSize) { // Save errore message to "ViewData", dictionary whico offers one way to pass data from controller to View. ViewData["file_errormessage"] = "File exceeds maximus size allowance of " + maxFileSize + "!"; return(View("Create")); } foreach (var file in uploadedFile) { // Check again if file is approriate size if (file.Length > 0) { // Get name of logged-in user var loggedInUser = getLoggedInUser(); // Combine path to specfic file var uploads = Path.Combine(_environment.WebRootPath, "uploads"); // File name without extension var pureFileName = Path.GetFileNameWithoutExtension(Path.Combine(uploads, file.FileName)); // Check file extension var fileExtension = Path.GetExtension(Path.Combine(uploads, file.FileName)); if (verfiyExtension(fileExtension)) { // Assemble path to where uploaded file will be saved var savePath = uploads + "\\" + loggedInUser + "\\" + fileUpload.caseName; // Assemble path to where mesh and visualization directory are to be created var meshVisuPath = uploads + "\\" + loggedInUser + "\\" + fileUpload.caseName + "\\" + pureFileName;; // Check if file already exists in user's database foreach (var existingUpload in _context.fileUpload) { foreach (var sameUser in existingUpload.userName) { if (existingUpload.caseName == fileUpload.caseName) { // Save errore message to "ViewData", dictionary whico offers one way to pass data from controller to View. ViewData["case_errormessage"] = "Case name you have entered already exists on list of your experiments!"; return(View("Create")); } } } // Obtain directory info of given path DirectoryInfo dirInfo = new DirectoryInfo(savePath); // Check if directory exists if (!dirInfo.Exists) { dirInfo = Directory.CreateDirectory(savePath); // Create stream with specified save-path and mode at which we operate with file using (var fileStream = new FileStream(Path.Combine(savePath, file.FileName), FileMode.Create)) { fileUpload.uploadDate = DateTime.Now; fileUpload.inputFilename = file.FileName; fileUpload.userName = loggedInUser; fileUpload.status = "Uploaded"; await file.CopyToAsync(fileStream); } // Meshing stage needs to be initialized as well // Create new meshingStage in database and assign // status to "initialized" meshingStage newMesh = new meshingStage(); newMesh.caseName = fileUpload.caseName; newMesh.ID = fileUpload.ID; newMesh.inputFilename = fileUpload.inputFilename; newMesh.uploadDate = fileUpload.uploadDate; newMesh.userName = fileUpload.userName; newMesh.status = "initialized"; // Add file to _context variable _context.Add(fileUpload); _context.Add(newMesh); await _context.SaveChangesAsync(); // Set status of file to uploaded! return(RedirectToAction("Index")); } else { // Save errore message to "ViewData", dictionary whico offers one way to pass data from controller to View. ViewData["file_errormessage"] = "There has been error with directory creation in our database, please hold on for maintainance!"; return(View("Create")); } } else { // Save errore message to "ViewData", dictionary whico offers one way to pass data from controller to View. ViewData["file_errormessage"] = "File extension not appropriate! File has to be of .stl or .STL format"; return(View("Create")); // WORKS: //return RedirectToAction("Create", "fileUploads"); //looses VIEWDATA } } } } return(RedirectToAction("Index", "fileUploads")); }