public async Task OnPostAsync() { if (UploadedFile != null && UploadedFile.Length > 0) { if (Path.GetExtension(UploadedFile.FileName).ToLower() == ".csv") { // create the directory if it doesn't exists, before saving if (!Directory.Exists(_appSettings.FileUpload.DestinationPath)) { Directory.CreateDirectory(_appSettings.FileUpload.DestinationPath); } var filePath = Path.Combine(_appSettings.FileUpload.DestinationPath, UploadedFile.FileName); using (var fileStream = new FileStream(filePath, FileMode.Create)) { await UploadedFile.CopyToAsync(fileStream); } await _fileService.ImportFileAsync(filePath); } else { Message = $"Cannot upload '{UploadedFile.FileName}', only .csv files accepted"; UploadedFile = null; } } }
// To protect from overposting attacks, see https://aka.ms/RazorPagesCRUD public async Task <IActionResult> OnPostAsync() { if (!ModelState.IsValid) { return(Page()); } if (UploadedFile != null) { if (!System.IO.Directory.Exists("./wwwroot/Images/Uploads")) { Directory.CreateDirectory("./wwwroot/Images/Uploads"); } var file = "./wwwroot/Images/Uploads/" + UploadedFile.FileName; using (var fileStream = new FileStream(file, FileMode.Create)) { await UploadedFile.CopyToAsync(fileStream); } Product.ImageFile = "Uploads/" + UploadedFile.FileName; } foreach (var tag in _context.Tags) { var tagName = $"tag-{tag.Id}"; if (HttpContext.Request.Form[tagName].FirstOrDefault() != null) { Product.AllergyTags.Add(tag); } } _context.Products.Add(Product); await _context.SaveChangesAsync(); InventoryItem inventoryRecord = new() { ProductId = Product.Id, Quantity = 0 }; _context.Inventory.Add(inventoryRecord); await _context.SaveChangesAsync(); return(RedirectToPage("./Index")); } }
public async Task OnPostAsync() { if (UploadedFile == null || UploadedFile.Length == 0) { return; } string ext = UploadedFile.FileName.Split('.').Last(); _logger.LogInformation($"Zapisywanie {UploadedFile.FileName}."); File_guid = Guid.NewGuid().ToString(); File_name = $"{File_guid}.{ext}"; string targetFileName = $"{_environment.ContentRootPath}/wwwroot/TempFiles/{File_name}"; using (var stream = new FileStream(targetFileName, FileMode.Create)) { await UploadedFile.CopyToAsync(stream); } ExcelPackage.LicenseContext = LicenseContext.NonCommercial; FileInfo existingFile = new FileInfo(targetFileName); using (ExcelPackage excel = new ExcelPackage(existingFile)) { ExcelWorksheets ws = excel.Workbook.Worksheets; worksheets.Clear(); foreach (ExcelWorksheet sheet in ws) { worksheets.Add(new SelectListItem { Text = sheet.Name, Value = sheet.Name }); } } HttpContext.Session.SetString("nazwa_pliku", UploadedFile.FileName); Nazwa_pliku = UploadedFile.FileName; IsUploaded = true; _logger.LogInformation($"Zapisano jako {File_name}."); HttpContext.Session.SetString("dodano", "false"); }
public async Task OnPostAsync() { var filePath = Path.Combine(GetBaseUploadPath(), UploadedFile.FileName); _logger.LogInformation($"Saving file {UploadedFile.FileName} to {filePath}"); await using (var fs = new FileStream(filePath, FileMode.Create)) { await UploadedFile.CopyToAsync(fs); } Toast = new Toast { Message = $"Uploaded {UploadedFile.FileName}", Class = "alert-success" }; // Call the code or external process to send file var output = $"ls -al {GetBaseUploadPath()}".ExecuteBash(); _logger.LogInformation(output); }
// To protect from overposting attacks, enable the specific properties you want to bind to. // For more details, see https://aka.ms/RazorPagesCRUD. public async Task <IActionResult> OnPostAsync() { if (!ModelState.IsValid) { return(Page()); } _context.Attach(Product).State = EntityState.Modified; if (UploadedFile != null) { var file = "./wwwroot/Images/Uploads/" + UploadedFile.FileName; var deleteFile = "./wwwroot/images/" + Product.ImageFile; if (deleteFile.Contains("Uploads/")) { System.IO.File.Delete(deleteFile); } using (var fileStream = new FileStream(file, FileMode.Create)) { await UploadedFile.CopyToAsync(fileStream); } Product.ImageFile = "Uploads/" + UploadedFile.FileName; } var tagsWithProducts = _context.Tags.Include(p => p.Products); foreach (var tag in tagsWithProducts) { var tagName = $"tag-{tag.Id}"; if (HttpContext.Request.Form[tagName].FirstOrDefault() != null) { if (!tag.Products.Contains(Product)) { Product.AllergyTags.Add(tag); } } else if (tag.Products.Contains(Product)) { tag.Products.Remove(Product); } } try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!ProductExists(Product.Id)) { return(NotFound()); } else { throw; } } return(RedirectToPage("./Index")); }