Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

275
Visualizações
Display image returned as byte array from MVC controller through ajax request

I'm receiving a file object from an MVC controller as FileContentResult:

[HttpPost]
        public IActionResult Export(RequestModel request)
        {
          // Taking file entity fromdb
            var File = _Service.GetFileById(request);
            var cd = new ContentDispositionHeaderValue("attachment")
            {
                FileNameStar = File.FileName
            };
            Response.Headers.Add(Microsoft.Net.Http.Headers.HeaderNames.ContentDisposition, cd.ToString());
            return File(File.FileContent, File.ContentType);
        }
    }

In the client side "React js" I'm enabling the user to download the document directly via saveAs (npm library)

// Ajax request
 ApiClient.post("Api/Export", { id: parseInt(id) }).then(response => {
            var octetStreamMime = 'application/octet-stream';
            var contentType = response.headers['content-type'] || octetStreamMime;
            var data = [response.data]
            var file = new Blob(data, { type: contentType });
            console.log(file)
            var FileSaver = require('file-saver');
            FileSaver.saveAs(file, fileName);
        }).catch( err => console.log(err.response.data));
    }

When I download the file as text it works as expected, however with png/jpg/jpeg files it previews the image with the following error:

enter image description here

My question is, is my data corrupted in this scenario? Or the byte array received from MVC controller needs reformatting?

Thank you

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda