I have an MVC application in C # which is a WEb system. And I have a C # API that receives calls from the Site. I need to send a file from the Site to the API, I made the code as follows:
Site Code submitting to the API controller
public TRetorno Post<TEnvio, TRetorno>(TEnvio data, Stream arquivo)
{
using (var httpClient = this.CriarClient())
{
var httpRequest = new HttpRequestMessage
{
Method = HttpMethod.Post,
RequestUri = new Uri(this.endpoint)
};
var content = new MultipartFormDataContent();
content.Add(new StreamContent(arquivo), "file", "filename");
content.Add(new ObjectContent<TEnvio>(data, new JsonMediaTypeFormatter()), "model");
httpRequest.Content = content;
using (var response = httpClient.SendAsync(httpRequest).Result)
{
this.AplicarTratamentoDeRetorno(response);
return JsonConvert.DeserializeObject<TRetorno>(response.Content.ReadAsStringAsync().Result);
}
}
}
API Driver Code
[AllowAnonymous]
[ActionName("IncluirArquivos")]
public HttpResponseMessage PostArquivos(ImportacaoCredoresModel model)
{
var file = HttpContext.Current.Request.Files["file"];
servicoDeAplicacaoDeImportacao.IncluirArquivoImportacao(model.CodigoCredor, file.InputStream, model.NomeArquivo);
var arquivos = servicoDeAplicacaoDeRecuperacaoDeImportacoes.ConsultarArquivosImportacao(model.CodigoCredor);
return Request.CreateResponse(HttpStatusCode.OK, ConversoesImportacao.ConverterEmArquivos(arquivos));
}
The problem is that when you get the parameter var file = HttpContext.Current.Request.Files["file"];
it is null
.
I put the same code on another PC and the parameter does not come null
, is it filled with the file? Could it be some IIS configuration? FrameWork version?