Upload C# API file

Asked

Viewed 984 times

0

I have a C#MVC application, 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 did the code as follows:

Sending Site Code to 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 Controller 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 by taking the parameter var file = HttpContext.Current.Request.Files["file"]; he is null.

I put in another PC the same code and the parameter does not come null, comes filled with the file? Can it be some IIS configuration? Framework version?

1 answer

0


Well, I solved my problem by updating the Framework. Net4.5.

Browser other questions tagged

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