1

Estou com um formulário para enviar dados para o meu servidor.

Nesse formulario tenho alguns campos de entrada e um para upload de arquivos.

Esse é o envio do meu formulario.

submit () {
  console.log(this.banners)
  const banners = new FormData()
  banners.append('banners', this.banners)
  this.$axios.post(api.insertBanner, banners).then(response => response.data)
    .catch(error => {
      console.log(error)
    })
},

console.log(this.banners) resulta em:

inserir a descrição da imagem aqui

Meu metodo POST:

public async Task<ActionResult> Post(BannersDTO banners)

BannersDTO.cs

public class BannersDTO
{
    public Microsoft.AspNetCore.Http.IFormFile Files { get; set; }
    public string TermoOrSku { get; set; }
    public bool InserirImediato { get; set; }
    public string DataAtivacao { get; set; }
    public string DataVigencia { get; set; }
}

Problema: Tenho recebido status code 400(badRequest), ou seja, preciso modificar a entrada pois ela não está correta. O que há de errado?

Se eu modifico as partes de código:

public async Task<ActionResult> Post(Microsoft.AspNetCore.Http.IFormFile Files)

banners.append('files', this.banners.files)

Meu código funciona bem, mas no caso eu só recebo o arquivo, e eu necessito também dos dados.

1 Resposta 1

1

Acabei recorrendo a um código não muito familiar(apenas foi me enviado um link com um código pronto).

Explicando:

Crie um model personalizado:

using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Microsoft.AspNetCore.Mvc.ModelBinding.Binders;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;

namespace TestMultipart.ModelBinding
{
    public class JsonWithFilesFormDataModelBinder : IModelBinder
    {
        private readonly IOptions<MvcJsonOptions> _jsonOptions;
        private readonly FormFileModelBinder _formFileModelBinder;

        public JsonWithFilesFormDataModelBinder(IOptions<MvcJsonOptions> jsonOptions, ILoggerFactory loggerFactory)
        {
            _jsonOptions = jsonOptions;
            _formFileModelBinder = new FormFileModelBinder(loggerFactory);
        }

        public async Task BindModelAsync(ModelBindingContext bindingContext)
        {
            if (bindingContext == null)
                throw new ArgumentNullException(nameof(bindingContext));

            // Retrieve the form part containing the JSON
            var valueResult = bindingContext.ValueProvider.GetValue(bindingContext.FieldName);
            if (valueResult == ValueProviderResult.None)
            {
                // The JSON was not found
                var message = bindingContext.ModelMetadata.ModelBindingMessageProvider.MissingBindRequiredValueAccessor(bindingContext.FieldName);
                bindingContext.ModelState.TryAddModelError(bindingContext.ModelName, message);
                return;
            }

            var rawValue = valueResult.FirstValue;

            // Deserialize the JSON
            var model = JsonConvert.DeserializeObject(rawValue, bindingContext.ModelType, _jsonOptions.Value.SerializerSettings);

            // Now, bind each of the IFormFile properties from the other form parts
            foreach (var property in bindingContext.ModelMetadata.Properties)
            {
                if (property.ModelType != typeof(IFormFile))
                    continue;

                var fieldName = property.BinderModelName ?? property.PropertyName;
                var modelName = fieldName;
                var propertyModel = property.PropertyGetter(bindingContext.Model);
                ModelBindingResult propertyResult;
                using (bindingContext.EnterNestedScope(property, fieldName, modelName, propertyModel))
                {
                    await _formFileModelBinder.BindModelAsync(bindingContext);
                    propertyResult = bindingContext.Result;
                }

                if (propertyResult.IsModelSet)
                {
                    // The IFormFile was sucessfully bound, assign it to the corresponding property of the model
                    property.PropertySetter(model, propertyResult.Model);
                }
                else if (property.IsBindingRequired)
                {
                    var message = property.ModelBindingMessageProvider.MissingBindRequiredValueAccessor(fieldName);
                    bindingContext.ModelState.TryAddModelError(modelName, message);
                }
            }

            // Set the successfully constructed model as the result of the model binding
            bindingContext.Result = ModelBindingResult.Success(model);
        }
    }
}

Adicione como um DataAnotation

Banners.DTO

using Microsoft.AspNetCore.Mvc;

namespace LR.Manager.Banner.Entidades
{
    [ModelBinder(typeof(JsonWithFilesFormDataModelBinder), Name = "json")]
    public class BannersDTO
    {
        public Microsoft.AspNetCore.Http.IFormFile Files { get; set; }
        public string TermoOrSku { get; set; }
        public bool InserirImediato { get; set; }
        public string DataAtivacao { get; set; }
        public string DataVigencia { get; set; }
    }
}

No controller:

[HttpPost]
 public async Task<ActionResult> Post(BannersDTO banners)
 {

Modifique a requição:

submit () {
  let data = {}
  const { termoOrSku, inserirImediato, dataAtivacao, dataVigencia } = this.banners
  data.termoOrSku = termoOrSku
  data.inserirImediato = inserirImediato
  data.dataAtivacao = dataAtivacao
  data.dataVigencia = dataVigencia

  const files = new FormData()
  files.append('files', this.banners.files)
  files.append('json', JSON.stringify(data))
  this.$axios.post(api.insertBanner, files).then(response => response.data)
    .catch(error => {
      console.log(error)
    })
},

Vejam que eu tive que criar um form-data com o Arquivo e outro com o JSON. Pelo pouco que entendi do model personalizado o json é deserializado e um map é feito propriedade a propriedade.

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .