public CreateModel(WebPortalAPI.Data.ApplicationDbContext context, IMapper mapper, IHostingEnvironment env) { _context = context; _env = env; _mapper = mapper; FileUpload = new FileUploadVM(_context, _env); }
public EditModel(WebPortalAPI.Data.ApplicationDbContext context, IHostingEnvironment env, IMapper mapper) { _context = context; _mapper = mapper; _env = env; fileuploadVM = new FileUploadVM(_context, _env); }
public async Task <IActionResult> Upload(FileUploadVM model) { if (ModelState.IsValid) { string filePath = null; string fileName = null; if (model.CsvFile != null) { string uploadsFolder = Path.Combine(_hostingEnvironment.WebRootPath, "data"); fileName = model.CsvFile.FileName; filePath = Path.Combine(uploadsFolder, model.CsvFile.FileName); using (Stream fileStream = new FileStream(filePath, FileMode.Create)) { await model.CsvFile.CopyToAsync(fileStream); } } CSVFile csvFile = new CSVFile { FilePath = filePath, FileName = fileName }; _fileService.Add(csvFile); return(RedirectToAction("details", new { Id = csvFile.Id })); } return(View()); }
public async Task <IViewComponentResult> InvokeAsync(long courseId) { var model = new FileUploadVM { CourseId = courseId }; return(View(model)); }
private async Task <FileUploadVM> LoadAllFiles() { var viewModel = new FileUploadVM(); viewModel.FilesOnDatabase = await context.FilesOnDatabase.ToListAsync(); viewModel.FilesOnFileSystem = await context.FilesOnFileSystem.ToListAsync(); return(viewModel); }
public async Task <IViewComponentResult> InvokeAsync(long id, long courseId, long?parentId) { var model = new FileUploadVM { Id = id, CourseId = courseId, ParentSectionId = parentId > 0 ? parentId : null }; return(View(model)); }
public async Task <IActionResult> UploadAvatarImage(FileUploadVM model) { IFormFile formFile = Request.Form.Files.FirstOrDefault(); if (ModelState.IsValid) { if (formFile.Length > 0) { if (!FileExtensions.IsImageType(formFile.ContentType)) { return(BadRequest("The file provided is not of an acceptable format.")); } var userId = long.Parse(User.FindFirstValue(ClaimTypes.NameIdentifier)); var fileName = DateTime.Now.ToString("yyyyMMddHHmmss"); var filePath = $"{userId}"; var fileNamePath = $"{filePath}/{fileName}.{Path.GetExtension(formFile.FileName)}"; string mimeType = formFile.ContentType; byte[] fileData = new byte[formFile.Length]; using (var ms = new MemoryStream()) { await formFile.CopyToAsync(ms); fileData = ms.ToArray(); } try { _blobService.UploadFileToBlob(fileNamePath, fileData, mimeType); var entity = _context.Users.FirstOrDefault(x => x.Id == userId); if (entity != default) { if (entity.ProfileImage != null) { //Delete the old image from blob _blobService.DeleteBlobData(entity.ProfileImage); } //Set new image URL entity.ProfileImage = fileNamePath; //Update Entity and Save _context.Update(entity); await _context.SaveChangesAsync(); } } catch (Exception e) { _logger.LogError(e, $"Failed To Upload File For Course:{model.CourseId} Section:{model.Id}!"); } } } return(RedirectToAction("Index", "Account")); }
public async Task <IViewComponentResult> InvokeAsync() { var user = await _userManager.GetUserAsync(HttpContext.User); var model = new FileUploadVM { UserId = user.Id }; return(View(model)); }
public IActionResult Fileupload(List <IFormFile> files) { FileUploadVM f = new FileUploadVM(db, _env); f.file = files.FirstOrDefault(); f.FileName = files.FirstOrDefault().FileName; f.fileSize = files.FirstOrDefault().Length; f.Type = (int)Utils.FileType.Logo; f.save(); return(RedirectToAction("Index")); }
public async Task <IActionResult> UploadFile(FileUploadVM model) { IFormFile formFile = Request.Form.Files.FirstOrDefault(); if (ModelState.IsValid) { if (formFile.Length > 0) { if (!_fileOptions.AllowedExtensions.Contains(Path.GetExtension(formFile.FileName))) { return(BadRequest("The file provided is not of an acceptable format.")); } var fileName = DateTime.Now.ToString("yyyyMMddHHmmss"); var filePath = $"{model.CourseId}/{model.Id}"; var fileNamePath = $"{filePath}/{fileName}.{Path.GetExtension(formFile.FileName)}"; string mimeType = formFile.ContentType; byte[] fileData = new byte[formFile.Length]; using (var ms = new MemoryStream()) { await formFile.CopyToAsync(ms); fileData = ms.ToArray(); } try { _blobService.UploadFileToBlob(fileNamePath, fileData, mimeType); var entity = new FileModel { SectionId = model.Id, Name = model.Title, Uri = fileNamePath, MimeType = mimeType }; _context.Add(entity); await _context.SaveChangesAsync(); } catch (Exception e) { _logger.LogError(e, $"Failed To Upload File For Course:{model.CourseId} Section:{model.Id}!"); } } } return(RedirectToAction("Edit", "Author", new { id = model.CourseId, selectedSection = model.ParentSectionId })); }
public IActionResult InitDb(FileUploadVM fileToUploadVM) { if (fileToUploadVM.FileToUpload.FileName.ToLower() != "mondial.xml") { return(this.BadRequest("Bitte nur Datei mondial.xml laden")); } var stream = fileToUploadVM.FileToUpload.OpenReadStream(); string xml; using (var sr = new StreamReader(stream)) { xml = sr.ReadToEnd(); } world.InitDb(xml); return(RedirectToAction("Index")); }
public ActionResult UploadJson(FileUploadVM model) { //if (!ModelState.IsValid) // return View(model); byte[] data; using (MemoryStream ms = new MemoryStream()) { model.File.InputStream.CopyTo(ms); data = ms.ToArray(); } var json = Encoding.Default.GetString(data); var entity = JsonConvert.DeserializeObject <Product[]>(json); return(RedirectToAction("Index", "Home")); }
public async Task <IActionResult> OnPostAsync() { if (!ModelState.IsValid) { return(Page()); } FileUploadVM f = new FileUploadVM(_context, _env); f.file = files.FirstOrDefault(); f.FileName = files.FirstOrDefault().FileName; f.fileSize = files.FirstOrDefault().Length; f.Type = FileUpload.Type; f.save(); // _context.FileUploads.Add(FileUpload); //await _context.SaveChangesAsync(); return(RedirectToPage("./FileUploadIndex")); }
public ActionResult <ResultVM <bool> > UploadResume([FromForm] FileUploadVM fileUploadVM) { ResultVM <bool> resultVM = new ResultVM <bool>(); try { var formFile = fileUploadVM.Files.FirstOrDefault(); //Check if file size exceeds 2MB if (formFile.Length > (2 * 1024 * 1024)) { return(StatusCode(StatusCodes.Status400BadRequest, new { Codes = new string[] { "FileSizeExceeded" } })); } var result = resumeBusiness.UploadResume(formFile); mapper.Map(result, resultVM); return(resultVM); } catch (Exception ex) { resultVM.Message = ex.Message; resultVM.StatusCode = Convert.ToInt32(Enums.StatusCode.BadRequest); return(StatusCode(StatusCodes.Status400BadRequest, new { Result = resultVM })); } }
//Check Valid Image and Upload to Server public async Task <IActionResult> CheckImg(FileUploadVM vm) { var file = vm.File; if (file.Length > 0) { string errorMsg = null; String[] orgfileName = file.FileName.Split('.'); if (orgfileName[1].ToLower() != "jpg" && orgfileName[1].ToLower() != "jpeg" && orgfileName[1].ToLower() != "png") { errorMsg = "Only .jpg, .jpeg & .png are allowed"; return(Json(new { success = false, response = errorMsg })); } string path = Path.Combine(_environment.WebRootPath, "images/article"); var filename = orgfileName[0] + DateTime.Now.ToString("ddMMyyhhmmsstt") + "." + orgfileName[1]; using (var fs = new FileStream(Path.Combine(path, filename), FileMode.Create)) { await file.CopyToAsync(fs); } using (var img = Image.Load(Path.Combine(path, filename))) { vm.Source = $"/images/article/{filename}"; vm.Extension = Path.GetExtension(filename).Substring(1); vm.Width = img.Width; vm.Height = img.Height; vm.Size = file.Length / 1000; if (img.Height > 400) { if (img.Width > 700) { if (vm.Size < 10000) { return(Json(new { success = true, response = vm })); } else { errorMsg = "Image should be less than 10 MB"; } } else { errorMsg = "Image Width should be greater than 700px"; } } else { errorMsg = "Image height should be greater than 400px"; } if (errorMsg != null) { string fullPath = path + "/" + filename; if (System.IO.File.Exists(fullPath)) { System.IO.File.Delete(fullPath); } return(Json(new { success = false, response = errorMsg })); } } } return(BadRequest()); }