Prvi commit
This commit is contained in:
126
EveryThing/Pages/Projects/CreatePartItemUploadExcel.cshtml.cs
Normal file
126
EveryThing/Pages/Projects/CreatePartItemUploadExcel.cshtml.cs
Normal file
@@ -0,0 +1,126 @@
|
||||
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<IdentityApplicationUser> _userManager;
|
||||
private readonly SignInManager<IdentityApplicationUser> _loginManager;
|
||||
private readonly RoleManager<IdentityApplicationRole> _roleManager;
|
||||
|
||||
public CreatePartItemUploadExcelModel(ApplicationDbContext context, UserManager<IdentityApplicationUser> userManager, SignInManager<IdentityApplicationUser> loginManager, RoleManager<IdentityApplicationRole> 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<IActionResult> 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<IActionResult> OnPostUploadAsync(List<IFormFile> 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<IActionResult> OnPostUpload(int idProject, int idProjectPart, List<IFormFile> 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<string> uploadedFiles = new List<string>();
|
||||
|
||||
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});
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user