Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

274
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda