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

186
Views
wrong content type on blob cant open img / file is damaged

I am trying to download a File using the JS Blob() function, but i cannot open it. It is not clear what type beforehand as I got a filelist, the user can choose what to download. Mac says, that the format might be damaged and that it cannot open that file.

The cURL response I have, from which i create a blob is a binary string (at least i believe so because when i dump it, you see only random symbols and characters, as when you open an image with the text editor)

Here is the Image code as I have it via response from my cURL

and here is my code to download this Image.

                var a;
            a = document.createElement('a');

            a.href = URL.createObjectURL(new Blob([JSON.stringify(response)]));

           // here i tried with and without JSON.stringify
           // also tried new Blob([JSON.stringify(response)],{type:'image/jpeg'}) or with * instead of jpeg, nothing works. Does anybody happen to know what mistake i am doing?

            a.download = data.data.name;
            a.style.display = 'none';
            document.body.appendChild(a);
            a.click();

Been struggling with this for a few days now, any help is appreciated.

Also when i (in Mac Terminal) do the following command :

file test.jpeg

it returns

test.jpeg: data

Best Regards

EDIT: My PHP Code

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "https://myapi.com/webapp/api/v1/document/id/" . $id . "/download");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('AuthenticationToken:' . $token));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);


curl_close($ch);
echo $output;
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can try to serialize the image data to base64 string and pass it to the client

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "https://myapi.com/webapp/api/v1/document/id/" . $id . "/download");
curl_setopt($ch, CURLOPT_HTTPHEADER, array("AuthenticationToken:" . $token));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);

$contentType = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);

curl_close($ch);

$base64 = "data:/" . $contentType . ";base64," . base64_encode($output);

echo $base64;

It this case, on your client side you just need to put the response text for the href like this:

const a = document.createElement('a');
a.href = response;
a.download = data.data.name;
a.style.display = 'none';
document.body.appendChild(a);
a.click();
about 4 years ago · Juan Pablo Isaza Report
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!