什么是JavaScript Blob对象,为什么有用? [英] What is a JavaScript Blob object, why is it useful?

查看:54
本文介绍了什么是JavaScript Blob对象,为什么有用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近遇到了一个JavaScript Blob对象,我用它来初始化一个Web worker,其中的代码包含在文档的script标签中.

I recently ran into the JavaScript Blob object, I used it to initialize a web worker where the code was contained within a script tag in the document.

基于 MDN 文档:

Blob对象代表不可变的原始数据的类似文件的对象.Blob表示的数据不一定是JavaScript原生格式.

A Blob object represents a file-like object of immutable, raw data. Blobs represent data that isn't necessarily in a JavaScript-native format.

听起来像是在将所有共享一个MIME类型的东西放进麻袋里.我在这种观点上是错的,这种观点是不完整的吗?

It sounds like it acts as a sack to put a collection of things into that all share a MIME type. Am I wrong in this opinion, is this opinion incomplete?

为什么需要/有用的对象?

Why is the object needed/useful?

推荐答案

Blob 本身并不是非常有用.它们的有用之处在于它们可以处理许多旨在处理 File s的调用.其中最重要的是 URL.createObjectURL() ,可用于创建一个URL,然后可在HTML标签的 href src 属性中使用

Blobs aren't terribly useful on their own. What's useful about them is that they work with many calls which are meant to process Files. The most important of these is URL.createObjectURL(), which can be used to create a URL you can then use in the href or src attributes of HTML tags, @import statements and url() values in CSS, etc.

基本上, Blob 会为JavaScript提供类似于临时文件的内容,而 URL.createObjectURL()可让您将这些Blob当作是Web服务器上的文件一样对待.

Basically, Blob gives JavaScript something like temporary files, and URL.createObjectURL() lets you treat those blobs as though they were files on a web server.

这篇关于什么是JavaScript Blob对象,为什么有用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆