@page "/pageprojeto/{projetoId:long}" @attribute [Authorize] @rendermode InteractiveServer @using System.ComponentModel.DataAnnotations @using System.Text @using System.Text.Encodings.Web @using ConcursoProjetos.Service @using Microsoft.AspNetCore.Identity @using Microsoft.AspNetCore.WebUtilities @inject UserManager UserManager @inject AuthenticationStateProvider AuthenticationStateProvider @inject ICandidatoService candidatoService @inject IProjetoService projetoService @inject IDocumentoService documentoService @inject ITipoDocumentoService tipoDocumentoServico Cadastro de Projeto

Cadastro de Projeto

@foreach (var unidades in listaDeUnidades) { }
@*
@LargeUploadMessage
@if (UploadingLargeFile) { } *@ @if (_projeto != null && _candidato != null && _documentosModel != null) {

Envio de Anexos

@foreach (var model in _documentosModel) { }
Tipo de Arquivo Escolher o Arquivo Status
@model.TipoDocumento.Nome @if ((listaTipoDocumentoLoading.Count > 0) && listaTipoDocumentoLoading.Where(x => x.TipoDocumento.Id == model.TipoDocumento.Id).First().Uploading) { } @if (model.Status) { } else { }
}
@code { bool Uploading = false; bool UploadingLargeFile = false; string LargeUploadMessage = ""; long UploadedBytes; long TotalBytes; /// /// Occurs when a large file is selected or dropped /// /// /// async Task OnLargeFileInputFileChange(InputFileChangeEventArgs args, Candidato candidato, Projeto projeto, InputModelDocuments modelDocument) { Documento resultado; UploadedBytes = 0; // Disable the file input field //UploadingLargeFile = true; var tipoDocumentoLoading = listaTipoDocumentoLoading.Where(x => x.TipoDocumento.Id == modelDocument.TipoDocumento.Id).First(); tipoDocumentoLoading.Uploading = true; await InvokeAsync(StateHasChanged); // calculate the chunks we have to send TotalBytes = args.File.Size; long percent = 0; long chunkSize = 400000; long numChunks = TotalBytes / chunkSize; long remainder = TotalBytes % chunkSize; // get new filename with a bit of entropy string justFileName = Path.GetFileNameWithoutExtension(args.File.Name); string extension = Path.GetExtension(args.File.Name); string newFileNameWithoutPath = $"Projeto_{projeto.Id}_{modelDocument.TipoDocumento.Nome}-{justFileName}-{DateTime.Now.Ticks.ToString()}{extension}"; string projectPath = $"{Environment.CurrentDirectory}\\files\\{projeto.Id}"; string filename = $"{projectPath}\\{newFileNameWithoutPath}"; if (!Directory.Exists(projectPath)) { Directory.CreateDirectory(projectPath); } // Delete the file if it already exists in our \Files folder if (File.Exists(filename)) { File.Delete(filename); } // Open the input and output file streams using (var inStream = args.File.OpenReadStream(long.MaxValue)) { using (var outStream = File.OpenWrite(filename)) { while (UploadedBytes < TotalBytes) { var whatsLeft = TotalBytes - UploadedBytes; if (whatsLeft < chunkSize) chunkSize = remainder; // Read the next chunk var bytes = new byte[chunkSize]; var buffer = new Memory(bytes); var read = await inStream.ReadAsync(buffer); // Write it await outStream.WriteAsync(bytes, 0, read); // Update our progress data and UI UploadedBytes += read; percent = UploadedBytes * 100 / TotalBytes; // Report progress with a string LargeUploadMessage = $"Uploading {args.File.Name} {percent}%"; await InvokeAsync(StateHasChanged); } } } LargeUploadMessage = "Upload Complete."; //await ListFiles(); Documento documentoGravado = new() { Descricao = "", ArquivoNomeOriginal = newFileNameWithoutPath, ArquivoTamanhoBytes = TotalBytes, ProjetoId = projeto.Id, TipoDocumentoId = modelDocument.TipoDocumento.Id, UploadCompleto = true }; if (documentoGravado.Id > 0) // Alteração { documentoGravado.AtribuiId(modelDocument.DocumentoId); } resultado = documentoService.Gravar(documentoGravado); //UploadingLargeFile = false; tipoDocumentoLoading.Uploading = false; AtualizarListaDocumentos(); } [Parameter] public long projetoId { get; set; } private Candidato? _candidato; private Projeto? _projeto; private IEnumerable? _documentos; private IEnumerable? _documentosModel; private string _mensagem = "Loading..."; private long candidatoId; //private long projetoId = 5; List listaTipoDocumentoLoading = new(); [SupplyParameterFromForm] private InputModel Input { get; set; } = new(); private List listaDeUnidades = Enumerable.Range(2, 31).ToList(); protected override async Task OnInitializedAsync() { var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync(); var user = authState.User; if (user.Identity.IsAuthenticated) { var identityUser = await UserManager.GetUserAsync(user); if (identityUser != null) { _candidato = candidatoService.ObterPorUserID(identityUser.Id); } } if (projetoId != null && projetoId > 0) { _projeto = projetoService.ObterPorId(projetoId); _documentos = documentoService.Listar(_projeto.Id); AtualizarModel(_projeto); } AtualizarListaDocumentos(); var listaTipo = tipoDocumentoServico.Listar(); foreach (var tipo in listaTipo) listaTipoDocumentoLoading.Add(new ModelTipoDocumento { TipoDocumento = tipo, Uploading = false }); } private void AtualizarModel(Projeto projeto) { Input.Id = projeto.Id; Input.Nome = projeto.Nome; Input.NumeroUnidades = projeto.NumeroUnidades; Input.ResponsavelTecnicoCpf = projeto.ResponsavelTecnicoCpf; Input.ResponsavelTecnicoNomeCompleto = projeto.ResponsavelTecnicoNomeCompleto; Input.ResponsavelTecnicoTelefone = projeto.ResponsavelTecnicoTelefone; Input.ResponsavelTecnicoEmail = projeto.ResponsavelTecnicoEmail; } public async Task RegisterProjeto(EditContext editContext) { Projeto resultado; Projeto projeto = new(_candidato.Id) { Nome = Input.Nome, NumeroUnidades = Input.NumeroUnidades, ResponsavelTecnicoCpf = Input.ResponsavelTecnicoCpf, ResponsavelTecnicoNomeCompleto = Input.ResponsavelTecnicoNomeCompleto, ResponsavelTecnicoTelefone = Input.ResponsavelTecnicoTelefone, ResponsavelTecnicoEmail = Input.ResponsavelTecnicoEmail }; if (_projeto != null) // Alteração { projeto.AtribuiId(_projeto.Id); resultado = projetoService.Alterar(projeto); } else // Inclusão { resultado = projetoService.Adicionar(projeto); } if (resultado != null) { _projeto = resultado; projetoId = resultado.Id; MontarModelDocuments(); } StateHasChanged(); } private void AtualizarListaDocumentos() { if (_projeto == null || _projeto.Id <= 0) return; _documentos = documentoService.Listar(_projeto.Id); MontarModelDocuments(); StateHasChanged(); } private void MontarModelDocuments() { List resultado = new(); var listaTipo = tipoDocumentoServico.Listar(); foreach (TipoDocumento tipo in listaTipo) { long documentoId = 0; bool status = false; if (_documentos != null && _documentos.Count() > 0) { var ProjetoPossuiDocumento = _documentos.Where(d => d.TipoDocumentoId == tipo.Id).SingleOrDefault(); if (ProjetoPossuiDocumento != null) { documentoId = ProjetoPossuiDocumento.Id; status = true; } } resultado.Add(new InputModelDocuments { DocumentoId = documentoId, TipoDocumento = tipo, Status = status }); } _documentosModel = resultado; } private sealed class InputModel { public long Id { get; set; } [Required] [StringLength(100, MinimumLength = 5)] [Display(Name = "Nome do Projeto")] public string Nome { get; set; } = ""; [Required] [Range(2, 32, ErrorMessage = "O valor deve estar entre 2 e 32.")] [Display(Name = "Número de Unidades Habitacionais por Pavimento")] public int NumeroUnidades { get; set; } = 2; [Required] [MaxLength(100)] [Display(Name = "Nome do Responsável Técnico da Empresa")] public string ResponsavelTecnicoNomeCompleto { get; set; } [Required] [MaxLength(100)] [EmailAddress] [Display(Name = "E-mail do Responsável Técnico da Empresa")] public string ResponsavelTecnicoEmail { get; set; } [Required] [MaxLength(14)] [Display(Name = "CPF do Responsável Técnico da Empresa")] public string ResponsavelTecnicoCpf { get; set; } [Required] [MaxLength(20)] [Display(Name = "Telefone do Responsável Técnico da Empresa")] public string ResponsavelTecnicoTelefone { get; set; } } private sealed class InputModelDocuments { public long DocumentoId { get; set; } public TipoDocumento TipoDocumento { get; set; } //public DateTime DataInclusao { get; set; } public bool Status { get; set; } } private sealed class ModelTipoDocumento { public TipoDocumento TipoDocumento { get; set; } public bool Uploading { get; set; } } }