Skip to content

Commit e2814bb

Browse files
authored
Resolve conflicts + Update article
1 parent c3a8c93 commit e2814bb

File tree

1 file changed

+4
-28
lines changed

1 file changed

+4
-28
lines changed

4-binary/03-blob/article.md

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,12 @@
22

33
`ArrayBuffer` and views are a part of ECMA standard, a part of JavaScript.
44

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`.
66

77
`Blob` consists of an optional string `type` (a MIME-type usually), plus `blobParts` -- a sequence of other `Blob` objects, strings and `BufferSource`.
88

99
![](blob.svg)
1010

11-
Thanks to `type`, we can download/upload blobs, and it naturally becomes `Content-Type` in network requests.
12-
1311
The constructor syntax is:
1412

1513
```js
@@ -59,13 +57,9 @@ This behavior is similar to JavaScript strings: we can't change a character in a
5957
6058
A Blob can be easily used as an URL for `<a>`, `<img>` or other tags, to show its contents.
6159
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-
=======
6560
Thanks to `type`, we can also download/upload `Blob` objects, and the `type` naturally becomes `Content-Type` in network requests.
6661
6762
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
6963
7064
```html run
7165
<!-- download attribute forces the browser to download instead of navigating -->
@@ -78,13 +72,9 @@ link.href = URL.createObjectURL(blob);
7872
</script>
7973
```
8074

81-
We can also create a link dynamically in JavaScript and simulate a click by `link.click()`, then download starts authomatically.
75+
We can also create a link dynamically in JavaScript and simulate a click by `link.click()`, then download starts automatically.
8276

83-
<<<<<<< HEAD
84-
Here's the similar "on the fly" blob creation and download code, but without HTML:
85-
=======
8677
Here's the similar code that causes user to download the dynamicallly created `Blob`, without any HTML:
87-
>>>>>>> e074a5f825a3d10b0c1e5e82561162f75516d7e3
8878

8979
```js run
9080
let link = document.createElement('a');
@@ -99,39 +89,25 @@ link.click();
9989
URL.revokeObjectURL(link.href);
10090
```
10191

102-
<<<<<<< HEAD
103-
**`URL.createObjectURL` takes a blob and creates an unique URL for it, in the form `blob:<origin>/<uuid>`.**
104-
=======
10592
`URL.createObjectURL` takes a `Blob` and creates a unique URL for it, in the form `blob:<origin>/<uuid>`.
106-
>>>>>>> e074a5f825a3d10b0c1e5e82561162f75516d7e3
10793

108-
That's what the generated url looks like:
94+
That's what the value of `link.href` looks like:
10995

11096
```
11197
blob:https://javascript.info/1e67e00e-860d-40a5-89ae-6ab0cbee6273
11298
```
11399

114100
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`.
115101

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-
=======
119102
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
121103

122104
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.
123105

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-
=======
129106
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.
130107

131108
**So if we create a URL, that `Blob` will hang in memory, even if not needed any more.**
132109

133110
`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
135111

136112
In the last example, we intend the `Blob` to be used only once, for instant downloading, so we call `URL.revokeObjectURL(link.href)` immediately.
137113

@@ -257,7 +233,7 @@ While `ArrayBuffer`, `Uint8Array` and other `BufferSource` are "binary data", a
257233

258234
That makes Blobs convenient for upload/download operations, that are so common in the browser.
259235

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.
261237

262238
We can easily convert betweeen `Blob` and low-level binary data types:
263239

0 commit comments

Comments
 (0)