javascript - JS 中 Blob 对象和 File 对象有什么区别?

标签 javascript object

正如 MDN Web Docs 中所述:

The Blob object represents a blob, which is a file-like object of immutable, raw data; they can be read as text or binary data, or converted into a ReadableStream so its methods can be used for processing the data.

我还知道 File 对象继承了 Blob 的一些属性,并且它几乎可以在 Blob 可以使用的任何地方使用。但是,如果 File 可以与 Blob 在相同的上下文中使用,我应该如何在它们之间进行选择?在某些情况下,是否存在一种优于另一种的情况?

最佳答案

正如您链接的页面上所说:

The File interface is based on Blob, inheriting blob functionality and expanding it to support files on the user's system.

File docs表明除了 blob 属性之外,文件还具有 lastModified日期,a name ,并且可能是一条路径。它还指出

File objects are generally retrieved from a FileList object returned as a result of a user selecting files using the <input> element [or] from a drag and drop operation's DataTransfer object

只要您实际上可以选择使用哪一个,就意味着您正在自己构建它们。与 Blob constructor 相比, File constructor采用非可选的 name 参数。因此,每当您构建要为其指定文件名的内容时,请使用 File .

关于javascript - JS 中 Blob 对象和 File 对象有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61277500/

相关文章:

javascript - 在移动浏览器中使用 jquery 的位置

javascript 显示-隐藏特定时间的元素

javascript:window.print() 打印 2 页,而我有 1 页

javascript - 将对象与数组中的对象进行比较并替换键匹配的值

javascript - 使用参数对象将用户生成的值作为参数传递给函数时遇到问题

opencv - 训练级联分类器来检测论文

javascript - 如何将 language_in 或 jscomp_off 与 Google Closure Compiler Service API 一起使用

javascript - 如何对javascript对象的属性进行计算并打印出来

javascript - 如果JavaScript对象是通过引用传递的,为什么更新属性值不会更新局部变量

javascript - 使用 Medium Story API 在 Next.js 中上游图像响应失败