I load two blob files in JavaScript using the code below.
我使用下面的代码在JavaScript中加载两个blob文件。
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?
(blob1 === blob2)返回false,即使每个blob的报告大小为574字节。我究竟做错了什么?
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();
}
2 个解决方案
#1
0
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:
不幸的是,这是比较引用可比内容的两个完全独立的指针的情况。要了解这对于更简单的内容如何工作,请在javascript控制台中尝试以下操作:
new Number(5) == new Number(5)
新号码(5)==新号码(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.
返回false。令人沮丧的是作为一个对象的5不等于一个值为5的对象。但至少这会将Blob置于上下文中。
I'm running into this same problem, and agree with the previous suggestion that FileReader is the only option.
我遇到了同样的问题,并同意之前的建议,即FileReader是唯一的选择。
#2
0
You must use a FileReader to compare them
The documentation for FileReader is on MDN. Take a look at methods to choose what's best for your data.
FileReader的文档位于MDN上。看一下选择最适合您数据的方法。
A useless way of comparing two blobs is looking at their size. Although two blobs of the same size will return true no matter their content.
比较两个blob的无用方法是查看它们的大小。虽然两个相同大小的斑点无论其内容如何都会返回。
Example
new Blob([Math.random()]).size == new Blob([Math.random()]).size // true
.
new Blob([Math.random()])。size == new Blob([Math.random()])。size // true。
#1
0
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:
不幸的是,这是比较引用可比内容的两个完全独立的指针的情况。要了解这对于更简单的内容如何工作,请在javascript控制台中尝试以下操作:
new Number(5) == new Number(5)
新号码(5)==新号码(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.
返回false。令人沮丧的是作为一个对象的5不等于一个值为5的对象。但至少这会将Blob置于上下文中。
I'm running into this same problem, and agree with the previous suggestion that FileReader is the only option.
我遇到了同样的问题,并同意之前的建议,即FileReader是唯一的选择。
#2
0
You must use a FileReader to compare them
The documentation for FileReader is on MDN. Take a look at methods to choose what's best for your data.
FileReader的文档位于MDN上。看一下选择最适合您数据的方法。
A useless way of comparing two blobs is looking at their size. Although two blobs of the same size will return true no matter their content.
比较两个blob的无用方法是查看它们的大小。虽然两个相同大小的斑点无论其内容如何都会返回。
Example
new Blob([Math.random()]).size == new Blob([Math.random()]).size // true
.
new Blob([Math.random()])。size == new Blob([Math.random()])。size // true。