Upload files using ASP.NET MVC, AJAX and jQuery

Asked

Viewed 21 times

1

I have the following problem when uploading file to ASP.NET MVC with AJAX and jQuery

it enters the if that checks if Listings files != null

and arrives in Console.Writeline(source.Filename); to print the name of the file in the terminal

My debt and the following if my ajax is gone? or there is an error in the controller, because this error in the document variable at the time that takes it to the function that counts the characters of the file sent

       [HttpPost]
        public async Task<FormularioPrincipalViewModel> LerArquivo([FromForm]FormularioPrincipalViewModel model)//IList<IFormFile> arquivos
        {
            int totalDeCaracteres = 0;
            int quantidadeDeCaracteres = 0;
           //var documentos = new List<Documento>();  //Inutil por enquanto


            if (model.ListaArquivos != null)
            {
                foreach (IFormFile source in model.ListaArquivos)
                {
                    Console.WriteLine(source.FileName);
                    using MemoryStream output = new MemoryStream();
                    if (Models.Configuration.Documento.ValidarImagem(source.ContentType))
                    {
                        source.CopyTo(output);
                        quantidadeDeCaracteres = leitorDeImagem.Executar(output);
                        totalDeCaracteres += quantidadeDeCaracteres;
                    }


                    if (Models.Configuration.Documento.ValidarPdf(source.ContentType))
                    {
                        source.CopyTo(output);
                        quantidadeDeCaracteres = leitorDePdf.Executar(source.FileName, output);
                        System.Diagnostics.Debug.WriteLine("O Total de caracteres é {0}.", quantidadeDeCaracteres);
                        totalDeCaracteres += quantidadeDeCaracteres;
                    }


                    var documento = new Documento(source.ContentType, output.ToArray(), quantidadeDeCaracteres);

                    model.Orcamento.AdicionarDocumento(documento);
                    //documentos.Add(documento);
                    await documentoRepositorio.Adicionar(documento);//----------------------------------------------------------

                }
                

                model.QuantidadeDeCaracteres = totalDeCaracteres;
                Console.WriteLine(model.ListaArquivos.Count());
                Console.WriteLine(model.QuantidadeDeCaracteres);

            }
            return model;
        }

    var formdata = new FormData($('#arquivo').get(0));

    $("#btnAdd").on('click', function () {
        $.ajax({
            async: true,
            data: formdata, // $('#formlist').serialize(),
            type: "POST",
            url: '/Orcamento/AdicionarDocumento',
            success: function () {
              
                

            }

        })
    })

  • Which error? On which line? Put the error here that makes it easier...

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.