The attempt to use 'readAsArrayBuffer' on 'FileReader' was unsuccessful due to parameter 1 not being of type 'Blob'

I'm encountering an issue with a JavaScript FileReader that is giving me the error message Uncaught TypeError: Failed to execute 'readAsDataURL' on 'FileReader': parameter 1 is not of type 'Blob'.

Below is the snippet of JavaScript causing the problem:

var blob = null;
var xhr = new XMLHttpRequest(); 
xhr.open("GET", "C:\\Users\\yw1kew\\Desktop\\LG_FRAME.plmx"); 
xhr.responseType = "blob";
xhr.onload = function() {
    blob = xhr.response;
}
xhr.send();

var myReader = new FileReader();
myReader.readAsArrayBuffer(blob) // THE ERROR OCCURS HERE

Any suggestions on how to resolve this issue? Thank you.

Answer №1

After facing a similar issue, I spent hours scouring the internet for solutions until I stumbled upon the idea of using fetch, and it actually worked!

fetch("./file.bin")
  .then((data) => {
    return data.arrayBuffer();
  })
  .then((array) => {
    console.log(buffer);
  })
  .catch((error) => {
    console.log(error);
  });

Answer №2

For my situation, I rely on using SharePoint as a base and had to transform the binary code in this manner:

    decodeBinary = function (data) {
    var result = '';
    if (data) {
        var byteArray = new Uint8Array(data);
        for (var index = 0; index < data.byteLength; index++) {
            result = result + String.fromCharCode(byteArray[index]);
        }
    }
    return result;
};

Afterwards, it was utilized in the following way:

console.log(decodeBinary(arrayBuffer));

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is the Messenger Bot ignoring random users?

I am facing an issue that I need help explaining. The bot works perfectly when I use it from my own Facebook account. However, when I ask others to use it, the bot does not respond to them, even though I have received a green tick on the pages_messaging a ...

Mongoose retrieves the entire document, rather than just a portion of it

I'm currently experiencing an issue with my mongoose query callback from MongoDB. Instead of returning just a specific portion of the document, the code I'm using is returning the entire document. I have verified that in the database, the 'p ...

What is the best way to exclude a particular character from a text element utilizing jquery?

Looking to extract the numerical value from a div containing: <div class="balance"...>$500.48</div> The goal is to retrieve 500.48 as a number, not a string. One approach is to use alert($(".balance").text()) to verify that the content is ret ...

What steps can I take to prevent drawing on the HTML5 Canvas?

I am working on creating a Paint-like application using HTML 5 Canvas. In this project, I need to implement two types of rectangles: filled and unfilled. Here is the code snippet for achieving this functionality: function drawRectangle(isFilled = false) { ...

Feeling lost when it comes to tackling the Data Access Object/Layer in an Express/MongoDB setup?

I currently have an Express application that is integrated with MongoDB. My goal is to decouple my database access from the server layer. However, in trying to achieve this, I've encountered two main approaches: Passing Res as an argument //server.j ...

I am having trouble comprehending this JavaScript code, could someone provide me with assistance?

Currently delving into the world of JavaScript functions and stumbled upon this code snippet with the following output: Output: "buy 3 bottles of milk" "Hello master, here is your 0 change" function getMilk(money, costPerBottle) { ...

Display or conceal a div depending on the selected radio button

I am attempting to display a hidden div based on the input of a radio button. If the "yes" option is checked, the hidden div should be shown. Conversely, if the "no" option is checked, the div should be hidden. I'm not sure why this code isn't w ...

The error message "Angular Animate - TypeError: undefined is not a function" indicates that

I seem to be encountering an issue when loading my Angular App. "TypeError: undefined is not a function" After a bit of investigation, it appears that the problem lies in declaring ngAnimate in my controller and I noticed the error originating from the A ...

Is there a way to link the id selector with the item's id?

In my app, I have a list of items with buttons that can be liked. To ensure uniqueness, I am utilizing the id selector. My goal is to use the item's id and connect it to the id selector for proper distinction. How can I retrieve the id of each item a ...

Blogger's homepage URL obtained using JSON data

Please note: I don't have a background in programming. I'm making an effort to learn as much as possible. As I was reading (and later experimenting with) the solution to this query, it dawned on me that having the same information in JSON form ...

Jumping through pages using a For Loop in Angular 5

Angular 5 - Chrome. Seeking advice on preventing page jumping. I have a table connected to an ngFor loop. When I am at the top of my page and trigger an action that adds an element to the array, if that table with the ngFor loop is off-screen at the time ...

What is the reason for this failure to fasten?

Exploring the depths of Angular, I've come across numerous Stack Overflow posts on Angular binding. Despite that, I find myself grappling with the concept. Can someone shed some light on this issue using the following example? .controller(&ap ...

What is the process for updating the values of all objects within an array of objects using Javascript?

I am attempting to update the values of car in all objects within an array called data with the values from newData. Here is my code snippet: import "./styles.css"; const data = [ { id: 1, car: "Toyota 2020", owner: "BM" }, ...

Conceal the div when the horizontal scroll position of the container is at 0

Struggling with a JavaScript issue in a fluid width page that involves scrolling a div using navigation buttons. I am new to JavaScript and trying to use scrollLeft = 0 to hide the leftmost nav button if there's nothing to scroll, and to show the div ...

Having trouble getting jQuery click event to work with Express for loading a Handlebars page

My jQuery on click function is not working properly in conjunction with Express to route to a handlebars page and display the passed information. I have attempted different solutions such as changing the HTTP method from GET to POST, not using handlebars ...

Using NextJS: How to remove a script when the website URL includes the parameter ?unload-script

When using webdriver to test our website, an Intercom widget becomes embedded in the screenshot as the webdriver scrolls down. Attempts to hide Intercom via CSS have resulted in a javascript error: Cannot read properties of null (reading 'style') ...

Tips for simulating mouse events in Jasmine tests for Angular 2 or 4

New to Jasmine testing, I'm exploring how to test a directive that handles mouse events such as mouse down, up, and move. My main query is regarding passing mouse coordinates from the Jasmine spec to my directive in order to simulate the mouse events ...

Under what circumstances would you need to manually specify the displayName of a React component?

After coming across an article (linked here: https://medium.com/@stevemao/do-not-use-anonymous-functions-to-construct-react-functional-components-c5408ec8f4c7) discussing the drawbacks of creating React components with arrow functions, particularly regardi ...

What is the reason behind the limitation of resizing only the most recent page element I added?

On my webpage, I have implemented a feature where I can click on an image to enlarge it. Clicking on the image again shrinks it back to its original size. However, I am encountering an issue where only the latest image added to the screen is resizable, e ...

Tips for effectively passing navigation as props in React Navigation with Expo

How can I correctly pass navigation as props to another component according to the documentation? The navigation prop is automatically provided to each screen component in your app. Additionally, To type check our screens, we need to annotate the naviga ...