You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 4-binary/03-blob/article.md
+4-28Lines changed: 4 additions & 28 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,14 +2,12 @@
2
2
3
3
`ArrayBuffer` and views are a part of ECMA standard, a part of JavaScript.
4
4
5
-
In the browser, there are additional higher-level objects, described in [File API](https://www.w3.org/TR/FileAPI/).
5
+
In the browser, there are additional higher-level objects, described in [File API](https://www.w3.org/TR/FileAPI/), in particular `Blob`.
6
6
7
7
`Blob` consists of an optional string `type` (a MIME-type usually), plus `blobParts` -- a sequence of other `Blob` objects, strings and `BufferSource`.
8
8
9
9

10
10
11
-
Thanks to `type`, we can download/upload blobs, and it naturally becomes `Content-Type` in network requests.
12
-
13
11
The constructor syntax is:
14
12
15
13
```js
@@ -59,13 +57,9 @@ This behavior is similar to JavaScript strings: we can't change a character in a
59
57
60
58
A Blob can be easily used as an URL for `<a>`, `<img>` or other tags, to show its contents.
61
59
62
-
<<<<<<< HEAD
63
-
Let's start with a simple example. By clicking on a link you download a dynamically-generated blob with `hello world` contents as a file:
64
-
=======
65
60
Thanks to `type`, we can also download/upload `Blob` objects, and the `type` naturally becomes `Content-Type` in network requests.
66
61
67
62
Let's start with a simple example. By clicking on a link you download a dynamically-generated `Blob` with `hello world` contents as a file:
68
-
>>>>>>> e074a5f825a3d10b0c1e5e82561162f75516d7e3
69
63
70
64
```html run
71
65
<!-- download attribute forces the browser to download instead of navigating -->
For each URL generated by `URL.createObjectURL` the browser stores a URL -> `Blob` mapping internally. So such URLs are short, but allow to access the `Blob`.
115
101
116
-
<<<<<<< HEAD
117
-
A generated url is only valid while the current document is open. And it allows to reference the blob in `<img>`, `<a>`, any other object that expects an url.
118
-
=======
119
102
A generated URL (and hence the link with it) is only valid within the current document, while it's open. And it allows to reference the `Blob` in `<img>`, `<a>`, basically any other object that expects an url.
120
-
>>>>>>> e074a5f825a3d10b0c1e5e82561162f75516d7e3
121
103
122
104
There's a side-effect though. While there's a mapping for a `Blob`, the `Blob` itself resides in the memory. The browser can't free it.
123
105
124
-
<<<<<<< HEAD
125
-
The mapping is automatically cleared on document unload, so blobs are freed then. But if an app is long-living, then that doesn't happen soon. So if we create an URL, that blob will hang in memory, even if not needed any more.
126
-
127
-
**`URL.revokeObjectURL(url)` removes the reference from the internal mapping, thus allowing the blob to be deleted (if there are no other references), and the memory to be freed.**
128
-
=======
129
106
The mapping is automatically cleared on document unload, so `Blob` objects are freed then. But if an app is long-living, then that doesn't happen soon.
130
107
131
108
**So if we create a URL, that `Blob` will hang in memory, even if not needed any more.**
132
109
133
110
`URL.revokeObjectURL(url)` removes the reference from the internal mapping, thus allowing the `Blob` to be deleted (if there are no other references), and the memory to be freed.
134
-
>>>>>>> e074a5f825a3d10b0c1e5e82561162f75516d7e3
135
111
136
112
In the last example, we intend the `Blob` to be used only once, for instant downloading, so we call `URL.revokeObjectURL(link.href)` immediately.
137
113
@@ -257,7 +233,7 @@ While `ArrayBuffer`, `Uint8Array` and other `BufferSource` are "binary data", a
257
233
258
234
That makes Blobs convenient for upload/download operations, that are so common in the browser.
259
235
260
-
Methods that perform web-requests, such as [XMLHttpRequest](info:xmlhttprequest), [fetch](info:fetch-basics) and so on, can work with `Blob` natively, as well as with other binary types.
236
+
Methods that perform web-requests, such as [XMLHttpRequest](info:xmlhttprequest), [fetch](info:fetch) and so on, can work with `Blob` natively, as well as with other binary types.
261
237
262
238
We can easily convert betweeen `Blob` and low-level binary data types:
0 commit comments