Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

273
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!