I load two blob files in JavaScript using the code below.
I want to compare them to see if they are precisely the same.
(blob1 === blob2) is returning false, even though the reported size of each blob is 574 bytes. What am I doing wrong?
getHTTPAsBlob(url, callback) {
let cacheBust = Math.random().toString()
url = url + '?&cachebust=' + cacheBust
let xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.responseType = 'blob';
xhr.onload = function (e) {
if (xhr.status == 200) {
// get binary data as a response
let fileData = this.response;
let contentType = xhr.getResponseHeader("content-type")
var reader = new FileReader()
reader.onload = (e) => {
console.log(reader.result)
console.log(fileData)
callback(null, {
Body: reader.result,
Blob: fileData,
ContentType: contentType,
Etag: null,
LastModified: null,
})
}
reader.readAsText(fileData)
} else {
callback(xhr)
}
}
xhr.send();
}
Unfortunately, this is a case of comparing two completely independent pointers that reference comparable content. To see how this might work for simpler content, try the following in the javascript console:
new Number(5) == new Number(5)Returns false. Frustrating that 5 as an object does not equal an object whose value is 5. But at least this puts Blob into context.
I'm running into this same problem, and agree with the previous suggestion that FileReader is the only option.