正如 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 onBlob
, 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 aFileList
object returned as a result of a user selecting files using the<input>
element [or] from a drag and drop operation'sDataTransfer
object
只要您实际上可以选择使用哪一个,就意味着您正在自己构建它们。与 Blob
constructor 相比, File
constructor采用非可选的 name 参数。因此,每当您构建要为其指定文件名的内容时,请使用 File
.
关于javascript - JS 中 Blob 对象和 File 对象有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61277500/