1

This could be a relatively simple answer, and I may be just severely overlooking it.

Im using an API that packages some of our data and sends it back to me in a blob. It provides a url such as, blob:http://localhost:3001/somenumber.

Im assuming this url is where my file is? When i manually hit that url in my browser, it downloads the file I need(just a basic object of data). However I can't seem to actually get this file to output the data so I can handle it in my client code/javascript. I goofed around with the FileReader system but with no success.

Any help would be great. Basically, I want to take this blob url the DB service provides me, and read its contents so I can output the data to the view.

Thanks!

2
  • Have you tried using an XMLHttpRequest? Commented Feb 21, 2017 at 15:35
  • Lol...sometimes you just gotta laugh at your self. Thanks @lamelemon. This was it. Commented Feb 21, 2017 at 15:40

1 Answer 1

2

You can read the data like that :

var req = new XMLHttpRequest();
req.open("GET", "http://localhost:3001/somenumber", true);
req.responseType = "arraybuffer";

req.onreadystatechange = function(event) {
  if (req.readyState == 4 && req.status == 200) {
    var blob = new Blob([req.response], {type: "application/pdf"}); // or "image/png", or others...
    var fileURL = URL.createObjectURL(blob); // Create a temp ULR to the data (pdf, image...)
    location.href = fileURL; // switch location to this new URL
  }
};

You can take a look at https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Sending_and_Receiving_Binary_Data.

Sign up to request clarification or add additional context in comments.

1 Comment

Yup, im so silly. Thanks. A basic get request was all that it was. Thanks guys, @BNilsou.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.