Skip to content

Instantly share code, notes, and snippets.

@Prinzhorn
Last active January 1, 2020 18:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Prinzhorn/5a9d7db4e4fb9372b2e6 to your computer and use it in GitHub Desktop.
Save Prinzhorn/5a9d7db4e4fb9372b2e6 to your computer and use it in GitHub Desktop.
Internet Explorer 11 Blob from DataView throws InvalidStateError
var buffer = new ArrayBuffer(8);
var left = new DataView(buffer, 0, 4);
try {
//Throws InvalidStateError in IE 11.
//It does work if we use a specific view like Uint8Array and not the generic DataView contructor.
new Blob([left]);
} catch(ex) {
alert(ex.message);
}
@Prinzhorn
Copy link
Author

For my use case (passing the blob to createObjectURL) I could just replace DataView with Uint8Array to make it work in IE.

@tchansen
Copy link

I couldn't get it to work with Uint8Array for IE11 on Windows 8. Here is my solution:

      try {
        var blob = new Blob([data], {
          type: "text/csv;charset=utf-8"
        });
      } catch (e) {
        if (e.name == "InvalidStateError") {
          // InvalidStateError (tested on Win8 IE11)
          var bb = new MSBlobBuilder();
          bb.append(data);
          var blob = bb.getBlob('text/csv;charset=utf-8');
        } else {
          // We're screwed, blob constructor unsupported entirely   
        }
      }

@dfkaye
Copy link

dfkaye commented Jan 1, 2020

@tchansen - I tried your solution this morning and it works - thank you 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment