using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; using Microsoft.AspNetCore.Mvc.Rendering; using EveryThing.Data; using EveryThing.Models; using EveryThing.Models.Project; using Microsoft.AspNetCore.Http; using System.IO; using Microsoft.AspNetCore.Hosting; namespace EveryThing.Pages.Projects { [Authorize(Roles = "Administrator,ProjecThingUser")] public class CreatePartItemUploadExcelModel : PageModel { private readonly IWebHostEnvironment _hostingEnvironment; private readonly ApplicationDbContext _context; private readonly UserManager _userManager; private readonly SignInManager _loginManager; private readonly RoleManager _roleManager; public CreatePartItemUploadExcelModel(ApplicationDbContext context, UserManager userManager, SignInManager loginManager, RoleManager roleManager, IWebHostEnvironment environment) { _context = context; _userManager = userManager; _loginManager = loginManager; _roleManager = roleManager; _hostingEnvironment = environment; } [BindProperty] public IFormFile File { get; set; } [BindProperty] public int IdProject { get; set; } [BindProperty] public int IdProjectPart { get; set; } public IActionResult OnGet(int idProject, int idProjectPart) { var user = _userManager.GetUserAsync(User).Result; IdProject = idProject; IdProjectPart = idProjectPart; return Page(); } //public async Task OnPostAsync() //{ // if (!ModelState.IsValid) // { // return Page(); // } // string uploads = Path.Combine(_hostingEnvironment.WebRootPath, "uploads"); // if (File.Length > 0) // { // string filePath = Path.Combine(uploads, File.FileName); // using (Stream fileStream = new FileStream(filePath, FileMode.Create)) // { // await File.CopyToAsync(fileStream); // } // } // return RedirectToPage("./Edit"); //} //public async Task OnPostUploadAsync(List files) //{ // long size = files.Sum(f => f.Length); // foreach (var formFile in files) // { // if (formFile.Length > 0) // { // var filePath = Path.GetTempFileName(); // System.Diagnostics.Debug.WriteLine(filePath); // using (var stream = System.IO.File.Create(filePath)) // { // await formFile.CopyToAsync(stream); // } // } // } // // Process uploaded files // // Don't rely on or trust the FileName property without validation. // return RedirectToPage("./Edit"); //} public async Task OnPostUpload(int idProject, int idProjectPart, List postedFiles) { if (postedFiles == null || postedFiles.Count != 1) { return Page();//TODO return error } string path = Path.Combine(_hostingEnvironment.WebRootPath, "Uploads"); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } List uploadedFiles = new List(); var postedFile = postedFiles[0]; string fileName = Guid.NewGuid().ToString().Replace("-", "_") + Path.GetExtension(postedFile.FileName); using (FileStream stream = new FileStream(Path.Combine(path, fileName), FileMode.Create)) { postedFile.CopyTo(stream); uploadedFiles.Add(fileName); } return RedirectToPage("./CreatePartItemImportExcel", new { idProject = idProject, idProjectPart = idProjectPart, fileName = fileName}); } } }