-
Notifications
You must be signed in to change notification settings - Fork 44
/
TR.html
2115 lines (1829 loc) · 172 KB
/
TR.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>File API</title>
<meta name="revision" content="$Id$" />
<link rel="stylesheet" href="FileAPI.css" type="text/css" />
<script src="section-links.js" type="application/ecmascript"></script>
<script src="dfn.js" type="application/ecmascript"></script>
<!--[if IE]>
<style type='text/css'>
.ignore {
-ms-filter:"progid:DXImageTransform.Microsoft.Alpha(Opacity=50)";
filter: alpha(opacity=50);
}
</style>
<![endif]-->
<link rel="stylesheet" href="https://www.w3.org/StyleSheets/TR/W3C-WD" type="text/css" /></head>
<body><div class="head">
<p>
<a href="http://www.w3.org/"><img width="72" height="48" src="https://www.w3.org/Icons/w3c_home" alt="W3C"/></a>
</p>
<h1 class="title p-name" id="title">File API</h1>
<h2 id="w3c-working-draft-21-april-2015"><abbr title="World Wide Web Consortium">W3C</abbr> Working Draft <em>21 April 2015</em></h2>
<dl>
<dt>This version:</dt>
<dd><a class="u-url" href="http://www.w3.org/TR/2015/WD-FileAPI-20150421/">http://www.w3.org/TR/2015/WD-FileAPI-20150421/</a></dd>
<dt>Latest published version:</dt>
<dd><a href="http://www.w3.org/TR/FileAPI/">http://www.w3.org/TR/FileAPI/</a></dd>
<dt>Latest editor's draft:</dt>
<dd><a href="https://w3c.github.io/FileAPI/">https://w3c.github.io/FileAPI/</a></dd>
<dt>Test suite:</dt>
<dd><a href="http://w3c-test.org/FileAPI/">http://w3c-test.org/FileAPI/</a></dd>
<dt>Previous version:</dt>
<dd><a rel="dcterms:replaces" href="http://www.w3.org/TR/2013/WD-FileAPI-20130912/">http://www.w3.org/TR/2013/WD-FileAPI-20130912/</a></dd>
<dt>Editors:</dt>
<dd class="p-author h-card vcard"><span><a class="u-url url p-name fn" href="http://arunranga.com/">Arun Ranganathan</a>, Mozilla Corporation, <arun@mozilla.com></span>
</dd>
<dt>Repository and Participation:</dt>
<dd>
<a href="https://github.com/w3c/FileAPI/">We are on github.</a></dd>
<dd>
<a href="https://github.com/w3c/FileAPI/issues">File a bug/issue.</a></dd>
<dd>
<a href="https://github.com/w3c/FileAPI/commits">Commit history.</a> </dd>
<dd>
<a href="https://www.w3.org/Search/Mail/Public/search?keywords=&hdr-1-name=subject&hdr-1-query=FileAPI&index-grp=Public_FULL&index-type=t&type-index=public-webapps">Mailing list search.</a> </dd>
</dl>
<p class="copyright">
<a href="http://www.w3.org/Consortium/Legal/ipr-notice#Copyright">Copyright</a> ©
2015
<a href="http://www.w3.org/"><abbr title="World Wide Web Consortium">W3C</abbr></a><sup>®</sup>
(<a href="http://www.csail.mit.edu/"><abbr title="Massachusetts Institute of Technology">MIT</abbr></a>,
<a href="http://www.ercim.eu/"><abbr title="European Research Consortium for Informatics and Mathematics">ERCIM</abbr></a>,
<a href="http://www.keio.ac.jp/">Keio</a>, <a href="http://ev.buaa.edu.cn/">Beihang</a>).
<abbr title="World Wide Web Consortium">W3C</abbr> <a href="http://www.w3.org/Consortium/Legal/ipr-notice#Legal_Disclaimer">liability</a>,
<a href="http://www.w3.org/Consortium/Legal/ipr-notice#W3C_Trademarks">trademark</a> and
<a href="http://www.w3.org/Consortium/Legal/copyright-documents">document use</a>
rules apply.
</p>
</div>
<hr />
<div>
<div class="section">
<h2>Abstract</h2>
<p>
This specification provides an <acronym title="Application Programming Interface">API</acronym>
for representing file objects in web applications, as well as programmatically selecting them and accessing their data. This includes:
</p>
<ul>
<li>A <a class="dfnref" href="#dfn-filelist">FileList</a> interface, which represents an array of individually selected files from the underlying system.
The user interface for selection can be invoked via <code><input type="file"></code>, i.e. when the
<code>input</code> element is in the <code>File Upload</code> state [<a href="#HTML">HTML</a>] .</li>
<li>A <a class="dfnref" href="#dfn-Blob">Blob</a> interface, which represents immutable raw binary data, and allows access to ranges of bytes within the
<a href="#dfn-Blob"><code>Blob</code></a> object as a separate <a class="dfnref" href="#dfn-Blob">Blob</a>.</li>
<li>A <a href="#dfn-file" class="dfnref">File</a> interface, which includes readonly informational attributes about a file
such as its name and the date of the last modification (on disk) of the file.</li>
<li>A <a href="#dfn-filereader" class="dfnref">FileReader</a> interface, which provides methods to read a
<a href="#dfn-file">File</a> or a <a href="#dfn-Blob">Blob</a>, and an event model to obtain the results of these reads.</li>
<li>A <a href="#url" class="dfnref">URL scheme</a> for use with binary data such as files, so that they can be referenced within web applications.</li>
</ul>
<p>Additionally, this specification defines objects to be used within threaded web applications for the synchronous reading of files.</p>
<p>The section on Requirements and Use Cases [<a href="#requirements">REQ</a>] covers the motivation behind this specification.</p>
<p>
This API is designed to be used in conjunction with other APIs and elements on the web platform,
notably: <a href="#XHR" class="dfnref">XMLHttpRequest</a> (e.g. with an overloaded <code>send()</code>
method for File or <a href="#dfn-Blob"><code>Blob</code></a> arguments), <code>postMessage</code>, <code>DataTransfer</code> (part
of the <em>drag and drop API</em> defined in [<a href="#HTML">HTML</a>]) and
<a href="#Workers">Web Workers</a>. Additionally, it should be possible to programmatically obtain a list of files from the
<code>input</code> element when it is
in the <code>File Upload state</code>[<a href="#HTML">HTML</a>].
These kinds of behaviors are defined in the appropriate affiliated specifications.
</p>
</div>
<div class="section">
<h2 id="status">Status of this Document</h2>
<p>
<em>This section describes the status of this document at the time of its publication.
Other documents may supersede this document. A list of current <abbr title="World Wide Web Consortium">W3C</abbr> publications and the
latest revision of this technical report can be found in the <a href="http://www.w3.org/TR/"><abbr title="World Wide Web Consortium">W3C</abbr> technical reports index</a> at
http://www.w3.org/TR/.</em>
</p>
If you have comments for this spec, please send them to
<a href="mailto:public-webapps@w3.org">public-webapps@w3.org</a>
with a Subject: prefix of <code>[FileAPI]</code>. See
<a href="https://www.w3.org/Bugs/Public/buglist.cgi?product=WebAppsWG&component=FileAPI&resolution=---">Bugzilla</a>
for this specification's open bugs.
<p>
This document was published by the <a href="http://www.w3.org/2008/webapps/">Web Applications Working Group</a> as a Working Draft.
This document is intended to become a <abbr title="World Wide Web Consortium">W3C</abbr> Recommendation.
If you wish to make comments regarding this document, please send them to
<a href="mailto:public-webapps@w3.org">public-webapps@w3.org</a>
(<a href="mailto:public-webapps-request@w3.org?subject=subscribe">subscribe</a>,
<a href="http://lists.w3.org/Archives/Public/public-webapps/">archives</a>).
All comments are welcome.
</p>
<p>
Publication as a Working Draft does not imply endorsement by the <abbr title="World Wide Web Consortium">W3C</abbr>
Membership. This is a draft document and may be updated, replaced or obsoleted by other
documents at any time. It is inappropriate to cite this document as other than work in
progress.
</p>
<p>
This document was produced by a group operating under the
<a id="sotd_patent" href="http://www.w3.org/Consortium/Patent-Policy-20040205/">5 February 2004 <abbr title="World Wide Web Consortium">W3C</abbr> Patent
Policy</a>.
<abbr title="World Wide Web Consortium">W3C</abbr> maintains a <a href="http://www.w3.org/2004/01/pp-impl/42538/status" rel="disclosure">public list of any patent
disclosures</a>
made in connection with the deliverables of the group; that page also includes
instructions for disclosing a patent. An individual who has actual knowledge of a patent
which the individual believes contains
<a href="http://www.w3.org/Consortium/Patent-Policy-20040205/#def-essential">Essential
Claim(s)</a> must disclose the information in accordance with
<a href="http://www.w3.org/Consortium/Patent-Policy-20040205/#sec-Disclosure">section
6 of the <abbr title="World Wide Web Consortium">W3C</abbr> Patent Policy</a>.
</p>
<p>This document is governed by the <a id="w3c_process_revision" href="http://www.w3.org/2014/Process-20140801/">1 August 2014 <abbr title="World Wide Web Consortium">W3C</abbr> Process Document</a>.
</p>
</div>
<div id="toc">
<h2>Table of Contents</h2>
<div class="toc">
<ul>
<li><a href="#introduction">1. Introduction</a></li>
<li><a href="#conformance">2. Conformance</a></li>
<li><a href="#dependencies">3. Dependencies</a></li>
<li><a href="#terminology">4. Terminology</a></li>
<li><a href="#blob">5. The Blob Interface and Binary Data</a>
<ul>
<li><a href="#constructorBlob">5.1. Constructors</a>
<ul>
<li><a href="#constructorParams">5.1.1. Constructor Parameters</a></li>
</ul></li>
<li><a href="#attributes-blob">5.2. Attributes</a></li>
<li><a href="#methodsandparams-blob">5.3. Methods and Parameters</a>
<ul>
<li><a href="#slice-method-algo">5.3.1. The slice method</a></li>
<li><a href="#close-method">5.3.2. The close method</a></li>
</ul>
</li>
</ul></li>
<li><a href="#file">6. The File Interface</a>
<ul><li><a href="#file-constructor">6.1 File Constructor</a></li>
<li><a href="#file-attrs">6.2. Attributes</a></li>
</ul></li>
<li><a href="#filelist-section">7. The FileList Interface</a>
<ul><li><a href="#attributes-filelist">7.1. Attributes</a></li>
<li><a href="#filelist-methods-params">7.2. Methods and Parameters</a></li>
</ul></li>
<li><a href="#reading-data-section">8. Reading Data</a>
<ul>
<li><a href="#readOperationSection">8.1 The Read Operation</a></li>
<li><a href="#blobreader-task-source">8.2. The File Reading Task Source</a></li>
<li><a href="#APIASynch">8.3. FileReader</a>
<ul>
<li><a href="#filereaderConstrctr">8.3.1. Constructors</a></li>
<li><a href="#event-handler-attributes-section">8.3.2. Event Handler Attributes</a></li>
<li><a href="#blobreader-state">8.3.3. FileReader States</a></li>
<li><a href="#reading-a-file">8.3.4. Reading a File or Blob</a>
<ul>
<li><a href="#filedata-attr">8.3.4.1. The result attribute</a></li>
<li><a href="#readAsDataURL">8.3.4.2. The readAsDataURL(blob) method</a></li>
<li><a href="#readAsDataText">8.3.4.3. The readAsText(blob, label) method</a></li>
<li><a href="#readAsArrayBuffer">8.3.4.4. The readAsArrayBuffer(blob) method</a></li>
<li><a href="#dfn-error-steps">8.3.4.5. Error Steps</a></li>
<li><a href="#abort">8.3.4.6. The abort() method</a></li>
</ul></li>
</ul></li>
<li><a href="#enctype">8.4. Determining Encoding</a></li>
<li><a href="#events">8.5. Events</a>
<ul>
<li><a href="#event-summary">8.5.1. Event Summary</a></li>
<li><a href="#eventInvariants">8.5.2. Summary of Event Invariants</a></li>
</ul>
</li>
<li><a href="#readingOnThreads">8.6. Reading on Threads</a>
<ul>
<li><a href="#FileReaderSync">8.6.1. The FileReaderSync Interface</a>
<ul>
<li><a href="#filereadersyncConstrctr">8.6.1.1. Constructors</a></li>
<li><a href="#readAsTextSync">8.6.1.2. The readAsText method</a></li>
<li><a href="#readAsDataURLSync-section">8.6.1.3. The readAsDataURL method</a></li>
<li><a href="#readAsArrayBufferSyncSection">8.6.1.4. The readAsArrayBuffer method</a></li>
</ul></li>
</ul></li>
</ul></li>
<li><a href="#ErrorAndException">9. Errors and Exceptions</a>
<ul>
<li><a href="#dfn-error-codes">9.1. Throwing an Exception or Returning an Error</a></li>
</ul></li>
<li><a href="#url">10. A URL for Blob and File reference</a>
<ul>
<li><a href="#use-cases-scheme">10.1. Requirements for a New Scheme</a></li>
<li><a href="#alternative-schemas">10.2. Discussion of Existing Schemes</a></li>
<li><a href="#DefinitionOfScheme">10.3. Definition of blob URL Scheme</a>
<ul>
<li><a href="#originOfBlobURL">10.3.1. Origin of Blob URLs</a></li>
<li><a href="#unicodeSerializationOfBlobURL">10.3.2. Unicode Serialization of a Blob URL</a></li>
<li><a href="#fragmentDiscussion">10.3.3. Discussion of Fragment Identifier</a></li>
</ul>
</li>
<li><a href="#requestResponseModel">10.4. Dereferencing Model for Blob URLs</a>
<ul>
<li><a href="#TwoHundredOK">10.4.1. 200 OK</a></li>
<li><a href="#processing-media-types">10.4.2 Processing Media Types</a></li>
<li><a href="#NetworkError">10.4.3. NetworkError Conditions</a></li>
<li><a href="#ProtocolExamples">10.4.4. Examples</a></li>
</ul>
</li>
<li><a href="#creating-revoking">10.5. Creating and Revoking a Blob URL</a>
<ul>
<li><a href="#createRevokeMethodsParams">10.5.1. Methods and Parameters</a></li>
<li><a href="#examplesOfCreationRevocation">10.5.2. Examples of Blob URL Creation and Revocation</a></li>
</ul>
</li>
<li><a href="#lifeTime">10.6. Lifetime of Blob URLs</a></li>
</ul></li>
<li><a href="#security-discussion">11. Security Considerations</a></li>
<li><a href="#requirements">12. Requirements and Use Cases</a></li>
<li><a href="#AppendixA">13. Appendix A</a>
</li>
<li><a href="#acknowledgements-section">14. Acknowledgements</a></li>
<li><a href="#references">15. References</a>
<ul>
<li><a href="#normative-references">15.1. Normative references</a></li>
<li><a href="#informative-references">15.2. Informative References</a></li>
</ul></li>
</ul>
</div>
</div>
<div id="sections">
<div id="introduction" class="section">
<h2>1. Introduction</h2>
<p class="norm">This section is informative.</p>
<p>
Web applications should have the ability to manipulate as wide
as possible a range of user input, including files that a user may wish to upload
to a remote server or manipulate inside a rich web application. This specification
defines the basic representations for files, lists of files, errors raised by access to files,
and programmatic ways to read files. Additionally, this specification also defines an interface that represents
"raw data" which can be asynchronously processed on the main thread of conforming user agents. The interfaces and API defined in this
specification can be used with other interfaces and APIs exposed to the web platform.
</p>
<p>
The <a href="#dfn-file"><code>File</code></a> interface represents file data typically obtained from the underlying (OS) file system, and the <a href="#dfn-Blob"><code>Blob</code></a> interface
("Binary Large Object" - a name originally introduced to web APIs in <a href="#Blob-REF">Google Gears</a>) represents immutable raw data. <a href="#dfn-file"><code>File</code></a> or
<a href="#dfn-Blob"><code>Blob</code></a> reads should happen asynchronously on the main thread, with an optional synchronous API used
within threaded web applications. An asynchronous API for reading files prevents blocking and UI "freezing" on a user
agent's main thread. This specification defines an asynchronous API based on an <em>event model</em> to read and access a <a href="#dfn-file"><code>File</code></a> or <a href="#dfn-Blob"><code>Blob</code></a>'s
data. A <a href="#dfn-filereader"><code>FileReader</code></a>
object provides asynchronous read methods to
access that file's data through event handler attributes and the firing of events. The use of events and event handlers allows separate code blocks the ability
to monitor the <em>progress of the read</em> (which is particularly useful for remote drives or mounted drives, where file access performance may vary from local drives)
and error conditions that may arise during reading of a file. An example will be illustrative.</p>
<div class="example"><div class="exampleHeader">Example</div>
<p>In the example below, different code blocks handle progress, error, and success conditions.</p>
<div class="block"><div class="blockTitleDiv"><span class="blockTitle">ECMAScript</span></div><div class="blockContent"><pre class="code"><code class="es-code">
function startRead() {
<span class="comment">// obtain input element through DOM</span>
var file = document.getElementById('file').files[0];
if(file){
getAsText(file);
}
}
function getAsText(readFile) {
var reader = new FileReader();
<span class="comment">// Read file into memory as UTF-16</span>
reader.readAsText(readFile, "UTF-16");
<span class="comment">// Handle progress, success, and errors</span>
reader.onprogress = updateProgress;
reader.onload = loaded;
reader.onerror = errorHandler;
}
function updateProgress(evt) {
if (evt.lengthComputable) {
<span class="comment">// evt.loaded and evt.total are ProgressEvent properties</span>
var loaded = (evt.loaded / evt.total);
if (loaded < 1) {
<span class="comment">// Increase the prog bar length</span>
<span class="comment">// style.width = (loaded * 200) + "px";</span>
}
}
}
function loaded(evt) {
<span class="comment">// Obtain the read file data</span>
var fileString = evt.target.result;
<span class="comment">// Handle UTF-16 file dump</span>
if(utils.regexp.isChinese(fileString)) {
<span class="comment">//Chinese Characters + Name validation</span>
}
else {
<span class="comment">// run other charset test</span>
}
<span class="comment">// xhr.send(fileString)</span>
}
function errorHandler(evt) {
if(evt.target.error.name == "NotReadableError") {
<span class="comment">// The file could not be read</span>
}
}
</code></pre></div></div>
</div>
</div>
<div id="conformance" class="section">
<h2>2. Conformance</h2>
<p>
Everything in this specification is normative except for
examples and sections marked as being informative.
</p>
<p>
The keywords “<span class="rfc2119">MUST</span>”,
“<span class="rfc2119">MUST NOT</span>”,
“<span class="rfc2119">REQUIRED</span>”,
“<span class="rfc2119">SHALL</span>”,
“<span class="rfc2119">SHALL NOT</span>”,
“<span class="rfc2119">RECOMMENDED</span>”,
“<span class="rfc2119">MAY</span>” and
“<span class="rfc2119">OPTIONAL</span>” in this document are to be
interpreted as described in
<cite><a href="http://www.ietf.org/rfc/rfc2119">Key words for use in RFCs to
Indicate Requirement Levels</a></cite>
<a href="#RFC2119">[RFC2119]</a>.
</p>
<p>
The following conformance classes are defined by this specification:
</p>
<dl>
<dt><dfn id="dfn-conforming-implementation">conforming user agent</dfn></dt>
<dd>
<p>
A user agent is considered to be a
<a class="dfnref" href="#dfn-conforming-implementation">conforming user agent</a>
if it satisfies all of the <span class="rfc2119">MUST</span>-,
<span class="rfc2119">REQUIRED</span>- and <span class="rfc2119">SHALL</span>-level
criteria in this specification that apply to implementations. This specification uses both the terms "conforming user agent" and "user agent" to refer to this product class.
</p>
<p>User agents may implement algorithms in this specifications in any way desired, so long as the end result is indistinguishable from the result that would be obtained
from the specification's algorithms.</p>
</dd>
</dl>
<p>User agents that use ECMAScript to implement the APIs defined in this specification must implement them in a manner consistent with the
ECMAScript Bindings defined in the Web IDL specification [<a href="#WebIDL">WEBIDL</a>] as this specification uses that specification and terminology. </p>
</div>
<div id="dependencies" class="section">
<h3>3. Dependencies</h3>
<p>This specification relies on underlying specifications.</p>
<dl><dt>DOM</dt>
<dd><p>A <a href="#dfn-conforming-implementation">conforming user agent</a> must support at least the subset of the functionality defined in DOM4 that this specification relies upon; in particular,
it must support <code>EventTarget</code>. [<a href="#DOM4">DOM4</a>] </p></dd>
<dt>Progress Events</dt>
<dd><p>A <a href="#dfn-conforming-implementation">conforming user agent</a> must support the Progress Events specification. Data access on read operations is enabled via Progress Events.[<a href="#ProgressEvents">ProgressEvents</a>] </p></dd>
<dt>HTML</dt>
<dd><p>A <a href="#dfn-conforming-implementation">conforming user agent</a> must support at least the subset of the functionality defined in HTML that this specification relies upon;
in particular, it must support <a href="#event-loops">event loops</a> and <a href="#event-handler-attributes">event handler attributes</a>. [<a href="#HTML">HTML</a>]</p></dd>
<dt>Web IDL</dt>
<dd><p>A <a href="#dfn-conforming-implementation">conforming user agent</a> must also be a conforming implementation of the IDL fragments in this specification,
as described in the Web IDL specification. [<a href="#WebIDL">WebIDL</a>] </p></dd>
<dt>Typed Arrays</dt>
<dd><p>A <a href="#dfn-conforming-implementation">conforming user agent</a> must support the Typed Arrays specification [<a href="#TypedArrays">TypedArrays</a>].</p></dd></dl>
<p>Parts of this specification rely on the Web Workers specification; for those parts of this specification, the Web Workers specification is a normative dependency.
[<a href="#Workers">Workers</a>]</p>
</div>
<div id="terminology" class="section">
<h2>4. Terminology</h2>
<div id="termsAndAlgos" class="section">
<h3>4.1 Terms </h3>
<p>The terms and algorithms <dfn id="document">document</dfn>,
<dfn id="unloading-document-cleanup-steps">unloading document cleanup steps</dfn>, <dfn id="event-handler-attributes">event handler attributes</dfn>,
<dfn id="event-handler-event-type">event handler event type</dfn>,
<dfn id="effectiveScriptOrigin">effective script origin</dfn>, <dfn id="incumbentSettingsObject">incumbent settings object</dfn>, <dfn id="event-loops">event loops</dfn>,
<dfn id="dfn-task">task</dfn>, <dfn id="task-source">task source</dfn>, <dfn id="df-URL">URL</dfn>, <dfn id="global-script-cleanup-jobs-list">global script cleanup jobs list</dfn>, <dfn id="global-script-cleanup-jobs">global script cleanup</dfn>,
<dfn id="queue-a-task">queue a task</dfn>, <dfn id="dfn-UTF-8">UTF-8</dfn>, <dfn id="dfn-UTF-16">UTF-16</dfn>. <dfn id="structured-clone">structured clone</dfn>, <dfn id="collect-a-sequence-of-characters">collect a sequence of characters</dfn> and <dfn id="converting-a-string-to-ASCII-lowercase">converting a string to ASCII lowercase</dfn> are as defined by the HTML specification [<a href="#HTML">HTML</a>]. </p>
<p>The terms <dfn id="origin">origin</dfn> and <dfn id="same-origin">same origin</dfn> are as defined by the Web Origin Concept specification [<a href="#WEBORIGIN">ORIGIN</a>].</p>
<p>When this specification says to <dfn id="terminate-an-algorithm">terminate an algorithm</dfn> the user agent must terminate the algorithm after finishing the step it is on, and return from it. Asynchronous <a href="#read-methods">read methods</a> defined in this specification may return before the algorithm in question is terminated, and can be terminated by an <a href="#dfn-abort"><code>abort()</code></a> call.</p>
<p>The term <dfn id="throw-an-exception">throw</dfn> in this specification, as it pertains to exceptions, is used as defined in the DOM4 specification [<a href="#DOM4">DOM4</a>].</p>
<p>The term <dfn id="byte">byte</dfn> in this specification is used as defined in the Encoding Specification [<a href="#encodingSpecification">Encoding Specification</a>].</p>
<p>The term <dfn id="chunk">chunk</dfn> in this specification is used as defined in the Streams Specification [<a href="#streamsSpecification">Streams Specification</a>.]</p>
<p>The term <dfn id="contextObject">context object</dfn> in this specification is used as defined in the DOM4 specification [<a href="#DOM4">DOM4</a>].</p>
<p>The terms <dfn id="URL">URL</dfn>, <dfn id="relativeURL">relative URL</dfn>, <dfn id="baseURL">base URL</dfn>, <dfn id="URLParser">URL parser</dfn>, <dfn id="basicURLParser">basic URL Parser</dfn>, <dfn id="scheme">scheme</dfn>, <dfn id="host">host</dfn>, <dfn id="relativeScheme">relative scheme</dfn>, <dfn id="scheme-data">scheme data</dfn>, and <dfn id="fragmentIDDef">fragment</dfn> are as defined by the WHATWG URL Specification [<a href="#URL-API">URL</a>].</p>
<p>The terms <dfn id="requestFetch">request</dfn>, <dfn id="responseFetch">response</dfn>, <dfn id="dfn-body">body</dfn> and <dfn id="cross-origin-request">cross-origin request</dfn> are as defined in the WHATWG Fetch Specification [<a href="#Fetch">Fetch Specification</a>].</p>
<p>The term <dfn id="UnixEpoch">Unix Epoch</dfn> is used in this specification to refer to the time 00:00:00 UTC on January 1 1970 (or 1970-01-01T00:00:00Z ISO 8601); this is the same time that is conceptually "0" in ECMA-262 [<a href="#ECMA-262">ECMA-262</a>].</p>
<p>The algorithms and steps in this specification use the following mathematical operations:</p>
<ul>
<li><p>max(a,b) returns the maximum of a and b, and is always performed on integers as they are defined in WebIDL [<a href="#WebIDL">WebIDL</a>]; in the case of max(6,4) the result is 6.
This operation is also defined in ECMAScript [<a href="#ECMA-262">ECMA-262</a>].</p></li>
<li><p>min(a,b) returns the minimum of a and b, and is always performed on integers as they are defined in WebIDL [<a href="#WebIDL">WebIDL</a>]; in the case of min(6,4) the result is 4.
This operation is also defined in ECMAScript [<a href="#ECMA-262">ECMA-262</a>].</p></li>
<li><p>Mathematical comparisons such as < (less than), ≤ (less than or equal to) and > (greater than) are as in ECMAScript
[<a href="#ECMA-262">ECMA-262</a>].</p></li>
</ul>
</div>
</div>
<div id="blob" class="section">
<h2>5. The Blob Interface and Binary Data</h2>
<p>
A <a href="#dfn-Blob"><code>Blob</code></a> object refers to a <a href="#byte">byte</a> sequence, and has a <a href="#dfn-size"><code>size</code></a> attribute which is the total number of bytes in the byte sequence, and a <a href="#dfn-type"><code>type</code></a> attribute, which is an ASCII-encoded string in lower case representing the media type of the <a href="#byte">byte</a> sequence.
</p>
<p>A <a href="#dfn-Blob"><code>Blob</code></a> must have a <dfn id="readabilityState">readability state</dfn>, which is one of <a href="#dfn-openState"><code>OPENED</code></a> or <a href="#dfn-closedState"><code>CLOSED</code></a>. A <a href="#dfn-Blob"><code>Blob</code></a> that refers to a <a href="#byte">byte</a> sequence, including one of 0 bytes, is said to be in the <dfn id="dfn-openState"><code>OPENED</code></dfn> <a href="#readabilityState">readability state</a>. A <a href="#dfn-Blob"><code>Blob</code></a> is said to be <dfn id="closed">closed</dfn> if its <a href="#dfn-close"><code>close</code></a> method has been called. A <a href="#dfn-Blob"><code>Blob</code></a> that is <a href="#closed">closed</a> is said to be in the <dfn id="dfn-closedState"><code>CLOSED</code></dfn> <a href="#readabilityState">readability state</a>.</p>
<p>Each <a href="#dfn-Blob"><code>Blob</code></a> must have an internal <dfn id="snapshot-state">snapshot state</dfn>, which must be initially set to the state of the underlying storage, if any such underlying storage exists, and must be preserved through <a href="#structured-clone">structured clone</a>. Further normative definition of <a href="#snapshot-state">snapshot state</a> can be found for <a href="#file">files</a>. </p>
<div class="block"><div class="blockTitleDiv"><span class="blockTitle">IDL</span></div><div class="blockContent"><pre class="code"><code class="idl-code">
<dfn id="dfn-Blob">[Constructor,
Constructor(sequence<(ArrayBuffer or ArrayBufferView or Blob or DOMString)> <a href="#dfn-blobParts" title="blobParts" class="dfnref">blobParts</a>, optional <a href="#dfn-BlobPropertyBag" title="BlobPropertyBag" class="dfnref">BlobPropertyBag</a> options), Exposed=Window,Worker]
interface Blob</dfn> {
readonly attribute unsigned long long <a href="#dfn-size" title="size" class="dfnref">size</a>;
readonly attribute DOMString <a href="#dfn-type" class="dfnref">type</a>;
readonly attribute boolean <a href="#dfn-isClosed" class="dfnref">isClosed</a>;
<span class="comment">//slice Blob into byte-ranged chunks</span>
Blob <a href="#dfn-slice" title="slice">slice</a>([Clamp] optional long long <a href="#dfn-start" class="dfnref" title="start">start</a>,
[Clamp] optional long long <a href="#dfn-end" class="dfnref" title="length">end</a>,
optional DOMString <a href="#dfn-contentTypeBlob">contentType</a>);
void <a href="#dfn-close" title="close">close</a>();
};
dictionary <dfn id="dfn-BlobPropertyBag">BlobPropertyBag</dfn> {
DOMString <a href="#dfn-BPtype" title="BPtype">type</a> = "";
};
</code></pre></div></div>
<div id="constructorBlob" class="section">
<h3>5.1. Constructors</h3>
<p>The <code>Blob()</code> constructor can be invoked with zero or more parameters. When the <code>Blob()</code> constructor is invoked, user agents must run the following <dfn id="blob-constructor-steps">Blob constructor steps</dfn>:</p>
<ol>
<li><p>If invoked with zero parameters, return a new <code>Blob</code> object with its <a href="#readabilityState">readability state</a> set to <a href="#dfn-openState"><code>OPENED</code></a>, consisting of 0 bytes, with <a href="#dfn-size"><code>size</code></a> set to 0, and with <a href="#dfn-type"><code>type</code></a> set to the empty string.</p></li>
<li><p>Otherwise, the constructor is invoked with a <a href="#dfn-blobParts"><code>blobParts</code></a> sequence. Let <var>a</var> be that sequence.</p></li>
<li><p>Let <var>bytes</var> be an empty sequence of bytes.</p></li>
<li><p>Let <var>length</var> be <var>a</var>'s length. For 0 ≤ <var>i</var> < <var>length</var>, repeat the following steps:</p>
<ol>
<li><p>Let <var>element</var> be the <var>i</var>th element of <var>a</var>.</p></li>
<li><p>If <var>element</var> is a <code>DOMString</code>, run the following substeps:</p>
<ol>
<li><p>Let <var>s</var> be the result of converting <var>element</var> to a sequence of Unicode characters [<a href="#Unicode">Unicode</a>] using the algorithm for doing so in WebIDL [<a href="#WebIDL">WebIDL</a>].</p></li>
<li><p>Encode <var>s</var> as <a href="#dfn-UTF-8" class="dfnrefs">UTF-8</a> and append the resulting bytes to <var>bytes</var>.</p></li>
</ol>
<div class="note"><div class="noteHeader">Note</div>
<p>The algorithm from WebIDL [<a href="#WebIDL">WebIDL</a>] replaces unmatched surrogates in an invalid <a href="#dfn-UTF-16" class="dfnrefs">UTF-16</a> string with U+FFFD replacement characters. Scenarios exist when the <a href="#dfn-Blob"><code>Blob</code></a> constructor may result in some data loss due to lost or scrambled character sequences. </p>
</div>
</li>
<li><p>If <var>element</var> is an <code>ArrayBufferView</code> [<a href="#TypedArrays">TypedArrays</a>], convert it to a sequence of <code>byteLength</code> bytes from the underlying <code>ArrayBuffer</code>, starting at the <code>byteOffset</code> of the <code>ArrayBufferView</code> [<a href="#TypedArrays">TypedArrays</a>], and append those bytes to <var>bytes</var>.</p></li>
<li><p>If <var>element</var> is an <code>ArrayBuffer</code> [<a href="#TypedArrays">TypedArrays</a>], convert it to a sequence of <code>byteLength</code> bytes, and append those bytes to <var>bytes</var>.</p></li>
<li><p>If <var>element</var> is a <code>Blob</code>, append the bytes it represents to <var>bytes</var>. The <a href="#dfn-type"><code>type</code></a> of the <code>Blob</code> array element is ignored.</p></li>
</ol>
</li>
<li><p>If the <a href="#dfn-BPtype"><code>type</code></a> member of the optional <code>options</code> argument is provided and is not the empty string, run the following sub-steps:</p>
<ol>
<li>Let <var>t</var> be the <a href="#dfn-BPtype"><code>type</code></a> dictionary member. If <var>t</var> contains any characters outside the range U+0020 to U+007E, then set <var>t</var> to the empty string and return from these substeps.</li>
<li>Convert every character in <var>t</var> to lowercase using the "<a href="#converting-a-string-to-ASCII-lowercase">converting a string to ASCII lowercase</a>" algorithm [<a href="#WebIDL">WebIDL</a>].</li>
</ol>
</li>
<li><p>Return a <code>Blob</code> object with its <a href="#readabilityState">readability state</a> set to <a href="#dfn-openState"><code>OPENED</code></a>, referring to <var>bytes</var> as its associated <a href="#byte">byte</a> sequence, with its <a href="#dfn-size"><code>size</code></a> set to the length of <var>bytes</var>, and its <a href="#dfn-type"><code>type</code></a> set to the value of <var>t</var> from the substeps above. </p>
<div class="note"><div class="noteHeader">Note</div><p>The type t of a <code>Blob</code> is considered a <em>parsable MIME type</em> if the ASCII-encoded string representing the Blob object's type, when converted to a byte sequence, does not return undefined for the <em>parse MIME type algorithm</em> [<a href="#MIMESNIFF">MIMESNIFF</a>].</p> </div>
</li>
</ol>
<div id="constructorParams" class="section">
<h4>5.1.1. Constructor Parameters</h4>
<p>The <code>Blob()</code> constructor can be invoked with the parameters below:</p>
<dl>
<dt id="dfn-blobParts">A <code>blobParts</code> <code>sequence</code></dt>
<dd>which takes any number of the following types of elements, and in any order:
<ul>
<li><p><code>ArrayBuffer</code> [<a href="#TypedArrays">TypedArrays</a>] elements.</p></li>
<li><p><code>ArrayBufferView</code> [<a href="#TypedArrays">TypedArrays</a>] elements.</p></li>
<li><p><a href="#dfn-Blob"><code>Blob</code></a> elements.</p></li>
<li><p><code>DOMString</code> [<a href="#WebIDL">WebIDL</a>] elements.</p></li>
</ul>
</dd>
<dt>An <em>optional</em> <a href="#dfn-BlobPropertyBag"><code>BlobPropertyBag</code></a></dt>
<dd>which takes one member:
<ul>
<li><dfn id="dfn-BPtype"><code>type</code></dfn>, the ASCII-encoded string in lower case representing the media type of the <code>Blob</code>. Normative conditions for this member are provided in the <a href="#blob-constructor-steps">Blob constructor steps.</a></li>
</ul>
</dd>
</dl>
</div>
<div class="example"><div class="exampleHeader">Example</div>
<p>Examples of constructor usage follow.</p>
<div class="block"><div class="blockTitleDiv"><span class="blockTitle">ECMAScript</span></div><div class="blockContent"><pre class="code"><code class="es-code">
<span class="comment">// Create a new Blob object</span>
var a = new Blob();
<span class="comment">// Create a 1024-byte ArrayBuffer</span>
<span class="comment">// buffer could also come from reading a File</span>
var buffer = new ArrayBuffer(1024);
<span class="comment">// Create ArrayBufferView objects based on buffer</span>
var shorts = new Uint16Array(buffer, 512, 128);
var bytes = new Uint8Array(buffer, shorts.byteOffset + shorts.byteLength);
var b = new Blob(["foobarbazetcetc" + "birdiebirdieboo"], {type: "text/plain;charset=UTF-8"});
var c = new Blob([b, shorts]);
var a = new Blob([b, c, bytes]);
var d = new Blob([buffer, b, c, bytes]);
</code></pre></div></div>
</div>
</div>
<div id="attributes-blob" class="section">
<h3>5.2. Attributes</h3>
<dl><dt id="dfn-size"><code>size</code></dt><dd><p>Returns the size of the <a href="#byte">byte</a> sequence in number of bytes. On getting, conforming user agents must return the total
number of bytes that can be read by a <a href="#dfn-filereader"><code>FileReader</code></a> or <a href="#dfn-FileReaderSync"><code>FileReaderSync</code></a> object, or 0 if the Blob has no bytes to be read. If the <code>Blob</code> has a readability state of <a href="#dfn-closedState"><code>CLOSED</code></a> then <code>size</code> must return 0.</p></dd>
<dt id="dfn-type"><code>type</code></dt>
<dd><p>The ASCII-encoded string in lower case representing the media type of the <code>Blob</code>. On getting, user agents must return the type of a <code>Blob</code> as an ASCII-encoded string in lower case, such that when it is converted to a <a href="#byte">byte</a> sequence, it is a <em>parsable MIME type</em> [<a href="#MIMESNIFF">MIMESNIFF</a>], or the empty string -- 0 bytes -- if the type cannot be determined. The <a href="#dfn-type"><code>type</code></a> attribute can be set by the web application itself through constructor invocation and through the <a href="#dfn-slice"><code>slice</code></a> call; in these cases, further normative conditions for this attribute are in the <a href="#blob-constructor-steps">Blob constructor steps</a>, the <a href="#file-constructor-steps">File Constructor steps</a>, and the <a href="#slice-method-algo">slice method algorithm</a> respectively. User agents can also determine the <a href="#dfn-type"><code>type</code></a> of a <a href="#dfn-Blob"><code>Blob</code></a>, especially if the <a href="#byte">byte</a> sequence is from an on-disk file; in this case, further normative conditions are in the <a href="#file-type-guidelines">file type guidelines.</a>.
</p></dd>
<dt id="dfn-isClosed"><code>isClosed</code></dt>
<dd><p>The boolean value that indicates whether the <code>Blob</code> is in the <a href="#dfn-closedState"><code>CLOSED</code></a> <a href="#readabilityState">readability state.</a> On getting, user agents must return <code>false</code> if the <code>Blob</code> is in the <a href="#dfn-openState"><code>OPENED</code></a> <a href="#readabilityState">readability state</a>, and <code>true</code> if the <code>Blob</code> is in the <a href="#dfn-closedState"><code>CLOSED</code></a> <a href="#readabilityState">readability state</a> as a result of the <a href="#dfn-close"><code>close</code></a> method being called.</p></dd>
</dl>
<div class="note"><div class="noteHeader">Note</div>
<p>Use of the <code>type</code> attribute informs the <a href="#encoding-determination">encoding determination</a> and <a href="#processing-media-types">parsing the Content-Type header</a> when <a href="#dereference">dereferencing</a> <a href="#DefinitionOfScheme">Blob URLs</a>.</p>
</div>
</div>
<div id="methodsandparams-blob" class="section">
<h3>5.3. Methods and Parameters</h3>
<div class="section" id="slice-method-algo">
<h4>5.3.1. The slice method</h4>
<p>The <dfn id="dfn-slice"><code>slice</code></dfn> method returns a new <a href="#dfn-Blob" class="dfnref"><code>Blob</code></a> object with bytes ranging from the optional <a href="#dfn-start"><code>start</code></a>
parameter upto but not including the optional <a href="#dfn-end"><code>end</code></a> parameter, and with a
<a href="#dfn-type" class="dfnref"><code>type</code></a> attribute that is the value of the optional <a href="#dfn-contentTypeBlob"><code>contentType</code></a> parameter. It must act as follows :</p>
<ol>
<li><p>Let <var>O</var> be the <a href="#dfn-Blob"><code>Blob</code></a> <a href="#contextObject">context object</a> on which the <code>slice</code> method is being called. </p></li>
<li><p>The optional <dfn id="dfn-start"><code>start</code></dfn> parameter is a value for the start point of a <a href="#dfn-slice" class="dfnref"><code>slice</code></a> call, and must be treated as a byte-order position, with the zeroth position representing the first byte.
User agents must process <a href="#dfn-slice"><code>slice</code></a> with
<code>start</code> <em>normalized</em> according to the following:</p>
<ol type="a">
<li><p>If the optional <code>start</code> parameter is not used as a parameter when making this call, let <var>relativeStart</var> be 0.</p></li>
<li><p>If <code>start</code> is negative, let <var>relativeStart</var> be <em>max((<a href="#dfn-size"><code>size</code></a> + <code>start</code>), 0)</em>.</p></li>
<li><p>Else, let <var>relativeStart</var> be <em>min(start, size)</em>.</p></li>
</ol>
</li>
<li><p>The optional <dfn id="dfn-end"><code>end</code></dfn> parameter is a value for the end point of a
<a href="#dfn-slice" class="dfnref"><code>slice</code></a> call. User agents must process <a href="#dfn-slice"><code>slice</code></a> with <code>end</code> normalized according to the following:</p>
<ol type="a">
<li><p>If the optional <code>end</code> parameter is not used as a parameter when making this call, let <var>relativeEnd</var> be <a href="#dfn-size"><code>size</code></a>.</p></li>
<li><p>If <code>end</code> is negative, let <var>relativeEnd</var> be <em>max((size + end), 0)</em> </p></li>
<li><p>Else, let <var>relativeEnd</var> be <em>min(end, size)</em></p></li>
</ol>
</li>
<li><p>The optional <dfn id="dfn-contentTypeBlob"><code>contentType</code></dfn> parameter is used to set the ASCII-encoded string in lower case representing the media type of the Blob. User agents must process the <a href="#dfn-slice"><code>slice</code></a> with <code>contentType</code> normalized according to the following:
</p>
<ol type="A">
<li><p>If the <code>contentType</code> parameter is not provided, let <var>relativeContentType</var> be set to the empty string .</p></li>
<li><p>Else let <var>relativeContentType</var> be set to <code>contentType</code> and run the substeps below: </p>
<ol>
<li>If <var>relativeContentType</var> contains any characters outside the range of U+0020 to U+007E, then set <var>relativeContentType</var> to the empty string and return from these substeps.</li>
<li>Convert every character in <var>relativeContentType</var> to lower case using the "<a href="#converting-a-string-to-ASCII-lowercase">Converting a string to ASCII lowercase</a>" algorithm.</li>
</ol>
</li>
</ol>
</li>
<li><p>Let <var>span</var> be <em>max((relativeEnd - relativeStart), 0)</em>.</p></li>
<li><p>Return a new <a href="#dfn-Blob"><code>Blob</code></a> object <var>S</var> with the following characteristics:</p>
<ol type="a">
<li><p><var>S</var> has a <a href="#readabilityState">readability state</a> equal to that of <var>O</var>'s <a href="#readabilityState">readability state</a>.</p>
<div class="note"><div class="noteHeader">Note</div><p>The <a href="#readabilityState">readability state</a> of the <a href="#contextObject">context object</a> is retained by the <a href="#dfn-Blob"><code>Blob</code></a> object returned by the <a href="#dfn-slice"><code>slice</code></a> call; this has implications on whether the returned
<a href="#dfn-Blob"><code>Blob</code></a> is actually usable for <a href="#readOperation">read operation</a>s or as a <a href="#DefinitionOfScheme">Blob URL</a>.</p></div>
</li>
<li><p><var>S</var> refers to <var>span</var> consecutive <a href="#byte">byte</a>s from <var>O</var>, beginning with the <a href="#byte">byte</a> at byte-order position <var>relativeStart</var>.</p></li>
<li><p><var>S</var>.<a href="#dfn-size"><code>size</code></a> = <var>span</var>.</p></li>
<li><p><var>S</var>.<a href="#dfn-type"><code>type</code></a> = <var>relativeContentType</var>.</p>
<div class="note"><div class="noteHeader">Note</div><p>The type t of a <code>Blob</code> is considered a <em>parsable MIME type</em> if the ASCII-encoded string representing the Blob object's type, when converted to a byte sequence, does not return undefined for the <em>parse MIME type algorithm</em> [<a href="#MIMESNIFF">MIMESNIFF</a>].</p> </div>
</li>
</ol>
</li>
</ol>
<div class="example"><div class="exampleHeader">Example</div>
<p>
The examples below illustrate the different types of <a href="#dfn-slice" class="dfnref"><code>slice</code></a> calls possible. Since the
<a href="#dfn-file"><code>File</code></a> interface inherits from the <a href="#dfn-Blob"><code>Blob</code></a> interface, examples are based on the use of the <a href="#dfn-file"><code>File</code></a> interface.
</p>
<div class="block"><div class="blockTitleDiv"><span class="blockTitle">ECMAScript</span></div><div class="blockContent"><pre class="code"><code class="es-code">
<span class="comment">// obtain input element through DOM</span>
var file = document.getElementById('file').files[0];
if(file)
{
<span class="comment">// create an identical copy of file</span>
<span class="comment">// the two calls below are equivalent</span>
var fileClone = file.slice();
var fileClone2 = file.slice(0, file.size);
<span class="comment">// slice file into 1/2 chunk starting at middle of file</span>
<span class="comment">// Note the use of negative number</span>
var fileChunkFromEnd = file.slice(-(Math.round(file.size/2)));
<span class="comment">// slice file into 1/2 chunk starting at beginning of file</span>
var fileChunkFromStart = file.slice(0, Math.round(file.size/2));
<span class="comment">// slice file from beginning till 150 bytes before end</span>
var fileNoMetadata = file.slice(0, -150, "application/experimental");
}
</code></pre></div></div>
</div>
</div>
<div id="close-method" class="section">
<h4>5.3.2. The close method</h4>
<p>The <dfn id="dfn-close"><code>close</code></dfn> method is said to <a href="#closed">close</a> a <a href="#dfn-Blob"><code>Blob</code></a>, and must act as follows on the <a href="#dfn-Blob"><code>Blob</code></a> on which the method has been called:</p>
<ol>
<li><p>If the <a href="#readabilityState">readability state</a> of the <a href="#contextObject">context object</a> is <a href="#dfn-closedState"><code>CLOSED</code></a>, <a href="#terminate-an-algorithm">terminate this algorithm</a>.</p></li>
<li><p>Otherwise, set the <a href="#readabilityState">readability state</a> of the <a href="#contextObject">context object</a> to <a href="#dfn-closedState"><code>CLOSED</code></a>.</p></li>
<li><p>If the <a href="#contextObject">context object</a> has an entry in the <a href="#BlobURLStore">Blob URL Store</a>, <a href="#removeTheEntry">remove the entry</a> that corresponds to the <a href="#contextObject">context object</a>.</p></li>
</ol>
</div>
</div>
</div>
<div id="file" class="section">
<h2>6. The File Interface</h2>
<p>
A <a href="#dfn-file"><code>File</code></a> object is a <a href="#dfn-Blob"><code>Blob</code></a> object with a <a href="#dfn-name"><code>name</code></a> attribute, which is a string; it can be created within the web application via a constructor, or is a reference to a <a href="#byte">byte</a> sequence from a file from the underlying (OS) file system.
</p>
<p>If a <a href="#dfn-file"><code>File</code></a> object is a reference to a <a href="#byte">byte</a> sequence originating from a file on disk, then its <a href="#snapshot-state">snapshot state</a> should be set to the state of the file on disk at the time the <a href="#dfn-file"><code>File</code></a> object is created. </p>
<div class="note"><div class="noteHeader">Note</div>
<p>This is a non-trivial requirement to implement for user agents, and is thus not a <em>must</em> but a <em>should</em> [<a href="#RFC2119">RFC2119</a>]. User agents should endeavor to have a <a href="#dfn-file"><code>File</code></a> object's <a href="#snapshot-state">snapshot state</a> set to the state of the underlying storage on disk at the time the reference is taken. If the file is modified on disk following the time a reference has been taken, the <a href="#dfn-file"><code>File</code></a>'s <a href="#snapshot-state">snapshot state</a> will differ from the state of the underlying storage. User agents may use modification time stamps and other mechanisms to maintain <a href="#snapshot-state">snapshot state</a>, but this is left as an implementation detail.</p>
</div>
<p>When a <a href="#dfn-file"><code>File</code></a> object refers to a file on disk, user agents must return the <a href="#dfn-type"><code>type</code></a> of that file, and must follow the <dfn id="file-type-guidelines">file type guidelines</dfn> below:</p>
<ul>
<li><p>User agents must return the <a href="#dfn-type"><code>type</code></a> as an ASCII-encoded string in lower case, such that when it is converted to a corresponding byte sequence, it is a <em>parsable MIME type</em> [<a href="#MIMESNIFF">MIMESNIFF</a>], or the empty string -- 0 bytes -- if the type cannot be determined.</p> </li>
<li><p>When the file is of type <code>text/plain</code> user agents must NOT append a charset parameter to the <em>dictionary of parameters</em> portion of the media type
[<a href="#MIMESNIFF">MIMESNIFF</a>]. </p></li>
<li><p>User agents must not attempt heuristic determination of encoding, including statistical methods. </p></li>
</ul>
<div class="block"><div class="blockTitleDiv"><span class="blockTitle">IDL</span></div><div class="blockContent"><pre class="code"><code class="idl-code">
<dfn id="dfn-file">[Constructor(sequence<(Blob or DOMString or ArrayBufferView or ArrayBuffer)> <a href="#dfn-fileBits" class="dfnrefs">fileBits</a>,
[EnsureUTF16] DOMString <a href="#dfn-fileName" class="dfnrefs">fileName</a>, optional FilePropertyBag options), Exposed=Window,Worker]
interface File</dfn> : <a href="#dfn-Blob" class="dfnref">Blob</a> {
readonly attribute DOMString <a href="#dfn-name" class="dfnref">name</a>;
readonly attribute long long <a href="#dfn-lastModified" class="dfnref">lastModified</a>;
};
dictionary <dfn id="dfn-FilePropertyBag">FilePropertyBag</dfn> {
DOMString <a href="#dfn-FPtype" title="FPtype">type</a> = "";
long long <a href="#dfn-FPdate" title="FPdate">lastModified</a>;
};
</code></pre></div></div>
<div id="file-constructor" class="section">
<h3>6.1 Constructor</h3>
<p>The <code>File</code> constructor is invoked with two or three parameters, depending on whether the optional dictionary parameter is used. When the <code>File()</code> constructor is invoked, user agents must run the following <dfn id="file-constructor-steps">File constructor steps</dfn>:</p>
<ol>
<li>Let <var>a</var> be the <code><a href="#dfn-fileBits">fileBits</a></code> sequence argument. Let <var>bytes</var> be an empty sequence of <a href="#byte">bytes</a>. Let <var>length</var> be <var>a</var>'s length. For 0 ≤ i < <var>length</var>, repeat the following steps:
<ol>
<li>Let <var>element</var> be the <var>i</var>'th element of <var>a</var>.</li>
<li>If <var>element</var> is a <code>DOMString</code>, run the following substeps:
<ol>
<li><p>Let <var>s</var> be the result of converting <var>element</var> to a sequence of Unicode characters [<a href="#Unicode">Unicode</a>] using the algorithm for doing so in WebIDL [<a href="#WebIDL">WebIDL</a>].</p></li>
<li><p>Encode <var>s</var> as <a href="#dfn-UTF-8" class="dfnrefs">UTF-8</a> and append the resulting <a href="#byte">bytes</a> to <var>bytes</var>.</p></li>
</ol>
<div class="note"><div class="noteHeader">Note</div>
<p>The algorithm from WebIDL [<a href="#WebIDL">WebIDL</a>] replaces unmatched surrogates in an invalid <a href="#dfn-UTF-16" class="dfnrefs">UTF-16</a> string with U+FFFD replacement characters. Scenarios exist when the <a id="dfnReturnLink-10" href="#dfn-Blob"><code>Blob</code></a> constructor may result in some data loss due to lost or scrambled character sequences. </p>
</div></li>
<li>If <var>element</var> is an <code>ArrayBufferView</code> [<a href="#TypedArrays">TypedArrays</a>], convert it to a sequence of <code>byteLength</code> bytes from the underlying ArrayBuffer, starting at the <code>byteOffset</code> of the <code>ArrayBufferView</code> [<a href="#TypedArrays">TypedArrays</a>], and append those bytes to <var>bytes</var>.</li>
<li><p>If <var>element</var> is an <code>ArrayBuffer</code> [<a href="#TypedArrays">TypedArrays</a>], convert it to a sequence of <code>byteLength</code> bytes, and append those bytes to <var>bytes</var>.</p></li>
<li><p>If <var>element</var> is a <code>Blob</code>, append the bytes it represents to <var>bytes</var>. The <a href="#dfn-type"><code>type</code></a> of the <code>Blob</code> argument must be ignored.</p></li>
</ol>
</li>
<li>Let <var>n</var> be a new string of the same size as the <code><a href="#dfn-fileName">fileName</a></code> argument to the constructor. Copy every character from <code><a href="#dfn-fileName">fileName</a></code> to <var>n</var>, replacing any "/" character (U+002F SOLIDUS) with a ":" (U+003A COLON).
<div class="note"><div class="noteHeader">Note</div>
<p>Underlying OS filesystems use differing conventions for file name; with constructed files, mandating UTF-16 lessens ambiquity when file names are converted to <a href="#byte">byte</a> sequences.</p>
</div>
</li>
<li>If the optional <a href="#dfn-FilePropertyBag"><code>FilePropertyBag</code></a> dictionary argument is used, then run the following substeps:
<ol>
<li>If the <a href="#dfn-FPtype"><code>type</code></a> member is provided and is not the empty string, let <var>t</var> be set to the <a href="#dfn-FPtype"><code>type</code></a> dictionary member. If <var>t</var> contains any characters outside the range U+0020 to U+007E, then set <var>t</var> to the empty string and return from these substeps. </li>
<li>Convert every character in <var>t</var> to lowercase using the "<a href="#converting-a-string-to-ASCII-lowercase">converting a string to ASCII lowercase algorithm</a>" [<a href="#WebIDL">WebIDL</a>].</li>
<li>If the <a href="#dfn-FPdate"><code>lastModified</code></a> member is provided, let <var>d</var> be set to the <a href="#dfn-FPdate"><code>lastModified</code></a> dictionary member. If it is not provided, set <var>d</var> to the current date and time represented as the number of milliseconds since the <a href="#UnixEpoch">Unix Epoch</a> (which is the equivalent of <code>Date.now()</code> [<a href="#ECMA-262">ECMA-262</a>]).
<div class="note"><div class="noteHeader">Note</div><p>Since ECMA-262 <code>Date</code> objects convert to <code>long long</code> values representing the number of milliseconds since the <a href="#UnixEpoch">Unix Epoch</a>, the <a href="#dfn-FPdate"><code>lastModified</code></a> member could be a <code>Date</code> object [<a href="#ECMA-262">ECMA-262</a>].</p></div>
</li>
</ol>
</li>
<li>Return a new <code>File</code> object <var>F</var> such that:
<ol>
<li><var>F</var> has a <a href="#readabilityState">readability state</a> of <a href="#dfn-openState"><code>OPENED</code></a>.</li>
<li><var>F</var> refers to the <var>bytes</var> <a href="#byte">byte</a> sequence.</li>
<li><code>F.size</code> is set to the number of total bytes in <var>bytes</var>.</li>
<li><code>F.name</code> is set to <var>n</var>.</li>
<li><code>F.type</code> is set to <var>t</var>.
<div class="note"><div class="noteHeader">Note</div><p>The type t of a <code>File</code> is considered a <em>parsable MIME type</em> if the ASCII-encoded string representing the File object's type, when converted to a byte sequence, does not return undefined for the <em>parse MIME type algorithm</em> [<a href="#MIMESNIFF">MIMESNIFF</a>].</p> </div>
</li>
<li><code>F.lastModified</code> is set to <var>d</var>.</li>
</ol>
</li>
</ol>
<div id="file-constructor-params">
<h4>6.1.1 Constructor Parameters</h4>
<p>The <code>File()</code> constructor can be invoked with the parameters below:</p>
<dl>
<dt id="dfn-fileBits">A <code>fileBits</code> <code>sequence</code></dt>
<dd>which takes any number of the following elements, and in any order:
<ul>
<li><p><code>ArrayBuffer</code> [<a href="#TypedArrays">TypedArrays</a>] elements.</p></li>
<li><p><code>ArrayBufferView</code> [<a href="#TypedArrays">TypedArrays</a>] elements.</p></li>
<li><p><a href="#dfn-Blob"><code>Blob</code></a> elements, which includes <a href="#dfn-file"><code>File</code></a> elements.</p></li>
<li><p><code>DOMString</code> [<a href="#WebIDL">WebIDL</a>] elements.</p></li>
</ul>
</dd>
<dt id="dfn-fileName">A <code>name</code> parameter</dt>
<dd>
<p>A <code>DOMString</code> [<a href="#WebIDL">WebIDL</a>] parameter representing the name of the file; normative conditions for this constructor parameter can be found in the <a href="#file-constructor-steps">File constructor steps</a>.</p></dd>
<dt id="def-Properties">An optional <a href="#dfn-FilePropertyBag"><code>FilePropertyBag</code></a> dictionary</dt><dd><p>which takes the following members:</p>
<ul>
<li><p>An optional <dfn id="dfn-FPtype"><code>type</code></dfn> member; the ASCII-encoded string in lower case representing the media type of the <code>File</code>. Normative conditions for this member are provided in the <a href="#file-constructor-steps">File constructor steps</a>.</p></li>
<li><p>An optional <dfn id="dfn-FPdate"><code>lastModified</code></dfn> member, which must be a <code>long long</code>; normative conditions for this member are provided in the <a href="#file-constructor-steps">File constructor steps</a>.</p></li>
</ul>
</dd>
</dl>
</div>
</div>
<div id="file-attrs" class="section">
<h3>6.2. Attributes</h3>
<dl>
<dt id="dfn-name"><code>name</code></dt>
<dd><p>The name of the file; on getting, this must return the name of the file as a string. There are numerous file name variations and conventions used by different underlying OS file systems;
this is merely the name of the file, without path information. On getting, if user agents cannot make this information available, they must return the empty string. If a <a href="#dfn-file"><code>File</code></a> object is created using a constructor, further normative conditions for this attribute are found in the <a href="#file-constructor-steps">file constructor steps</a>.</p>
</dd>
<dt id="dfn-lastModified"><code>lastModified</code></dt>
<dd><p>The last modified date of the file. On getting, if user agents can make this information available, this must return a <code>long long</code> set to the time the file was last modified as the number of milliseconds since the <a href="#UnixEpoch">Unix Epoch</a>. If the last modification date and time are not known, the attribute must return the current date and time as a <code>long long</code> representing the number of milliseconds since the <a href="#UnixEpoch">Unix Epoch</a>; this is equivalent to <code>Date.now()</code> [<a href="#ECMA-262">ECMA-262</a>]. If a <a href="#dfn-file"><code>File</code></a> object is created using a constructor, further normative conditions for this attribute are found in the <a href="#file-constructor-steps">file constructor steps</a>.
</p></dd>
</dl>
</div>
<p>The <code>File</code> interface is available on objects that expose an attribute of type <a href="#dfn-filelist"><code>FileList</code></a>; these objects are defined in
HTML [<a href="#HTML">HTML</a>]. The <code>File</code> interface, which inherits from <a href="#dfn-Blob"><code>Blob</code></a>, is immutable, and thus represents file data that can be
read into memory at the time a <a href="#read-methods">read operation</a> is initiated. User agents must process reads on files that no longer exist at the time of read as
<a href="#file-error-read">errors</a>, throwing a <a href="#dfn-NotFoundError"><code>NotFoundError</code></a> exception if using a <a href="#dfn-FileReaderSync"><code>FileReaderSync</code></a> on a Web Worker [<a href="#Workers">Workers</a>] or firing an <a href="#dfn-error-event"><code>error</code></a> event with the <a href="#dfn-error"><code>error</code></a>
attribute returning a <a href="#dfn-NotFoundError"><code>NotFoundError</code></a> <a href="#dfn-domerror"><code>DOMError</code></a>.</p>
<div class="example"><div class="exampleHeader">Example</div>
<p>In the examples below, metadata from a file object is displayed meaningfully, and a file object is created with a name and a last modified date.</p>
<div class="block"><div class="blockTitleDiv"><span class="blockTitle">ECMAScript</span></div><div class="blockContent"><pre class="code"><code class="es-code">
var file = document.getElementById("filePicker").files[0];
var date = new Date(file.lastModified);
println("You selected the file " + file.name + " which was modified on " + date.toDateString() + " .");
...
// Generate a file with a specific last modified date
var d = new Date(2013, 12, 5, 16, 23, 45, 600);
var generatedFile = new File(["Rough Draft ...."], "Draft1.txt", {type: "text/plain"; lastModified: d})
...
</code></pre></div></div>
</div>
<div id="filelist-section" class="section">
<h2>7. The FileList Interface</h2>
<div class="note"><p>The <code>FileList</code> interface should be considered "at risk" since the general trend on the Web Platform is to replace such interfaces with the <code>Array</code> platform object in ECMAScript [<a href="#ECMA-262">ECMA-262</a>]. In particular, this means syntax of the sort <code>filelist.item(0)</code> is at risk; most other programmatic use of <code>FileList</code> is unlikely to be affected by the eventual migration to an <code>Array</code> type. </p></div>
<p>
This interface is a list of <a href="#dfn-file"><code>File</code></a> objects.
</p>
<div class="block"><div class="blockTitleDiv"><span class="blockTitle">IDL</span></div><div class="blockContent"><pre class="code"><code class="idl-code">
[Exposed=Window,Worker] interface <dfn id="dfn-filelist">FileList</dfn> {
getter <a href="#dfn-file" class="dfnref">File</a>? <a href="#dfn-item" class="dfnref">item</a>(unsigned long <a href="#dfn-index" class="dfnref">index</a>);
readonly attribute unsigned long <a href="#dfn-length" class="dfnref">length</a>;
};
</code></pre></div></div>
<div class="example"><div class="exampleHeader">Example</div>
<p>
Sample usage typically involves DOM access to the <code><input type="file"></code> element within a form, and then accessing selected files.
</p>
<div class="block"><div class="blockTitleDiv"><span class="blockTitle">ECMAScript</span></div><div class="blockContent"><pre class="code"><code class="es-code">
<span class="comment">// uploadData is a form element</span>
<span class="comment">// fileChooser is input element of type 'file'</span>
var file = document.forms['uploadData']['fileChooser'].files[0];
<span class="comment">// alternative syntax can be</span>
<span class="comment">// var file = document.forms['uploadData']['fileChooser'].files.item(0);</span>
if(file)
{
<span class="comment">// Perform file ops</span>
}
</code></pre></div></div>
</div>
<div class="section" id="attributes-filelist">
<h3>7.1. Attributes</h3>
<dl>
<dt id="dfn-length"><code>length</code></dt><dd><p>must return the number of files in the <a href="#dfn-filelist"><code>FileList</code></a> object. If there are no files, this attribute must return 0.</p></dd>
</dl>
</div>
<div class="section" id="filelist-methods-params">
<h3>7.2. Methods and Parameters</h3>
<dl>
<dt id="dfn-item"><code>item(index)</code></dt><dd><p>must return the <em>indexth</em> <a href="#dfn-file"><code>File</code></a> object in
the <a href="#dfn-filelist"><code>FileList</code></a>. If there is no <em>indexth</em> <a href="#dfn-file"><code>File</code></a> object in the
<a href="#dfn-filelist"><code>FileList</code></a>, then this method must return <code>null</code>.</p>
<p><dfn id="dfn-index" title="index"><code>index</code></dfn> must be treated by user agents as value for the position of a <a href="#dfn-file"><code>File</code></a>
object in the <a href="#dfn-filelist"><code>FileList</code></a>, with 0 representing the first file. <strong>Supported property indices</strong> [<a href="#WebIDL">WebIDL</a>] are the numbers in the range zero to one less than the number of <a href="#dfn-file"><code>File</code></a> objects represented by the <code>FileList</code> object.
If there are no such <a href="#dfn-file"><code>File</code></a> objects, then there are no supported property indices [<a href="#WebIDL">WebIDL</a>].
</p>
</dd>
</dl>
</div>
<div class="note"><div class="noteHeader">Note</div>
The <code>HTMLInputElement</code> interface [<a href="#HTML">HTML</a>] has a readonly attribute of type <code>FileList</code>, which is what is
being accessed in the above example. Other interfaces with a readonly attribute of type <code>FileList</code> include the <code>DataTransfer</code>
interface [<a href="#HTML">HTML</a>].
</div>
</div>
</div>
<div id="reading-data-section" class="section">
<h2>8. Reading Data</h2>
<div id="readOperationSection" class="section">
<h3>8.1 The Read Operation</h3>
<p>The algorithm below defines a <a href="#readOperation">read operation</a>, which takes a <a href="#dfn-Blob"><code>Blob</code></a> and a <a href="#synchronousFlag">synchronous flag</a> as input, and reads <a href="#byte">byte</a>s into a byte stream which is returned as the <var>result</var> of the <a href="#readOperation">read operation</a>, or else fails along with a <a href="#failureReason">failure reason</a>. Methods in this specification invoke the <a href="#readOperation">read operation</a> with the <a href="#synchronousFlag">synchronous flag</a> either set or unset.</p>
<p>The <dfn id="synchronousFlag">synchronous flag</dfn> determines if a read operation is synchronous or asynchronous, and is <em>unset</em> by default. Methods may set it. If it is set, the <a href="#readOperation">read operation</a> takes place synchronously. Otherwise, it takes place asynchronously. </p>
<p>To perform a <dfn id="readOperation">read operation</dfn> on a <a href="#dfn-Blob"><code>Blob</code></a> and the <a href="#synchronousFlag">synchronous flag</a>, run the following steps:</p>
<ol>
<li><p>Let <var>s</var> be a a new <a href="#dfn-body">body</a>, <var>b</var> be the <a href="#dfn-Blob"><code>Blob</code></a> to be read from, and <var>bytes</var> initially set to an empty byte sequence. Set the <var>length</var> on <var>s</var> to the <a href="#dfn-size"><code>size</code></a> of <var>b</var>. While there are still bytes to be read in <var>b</var> perform the following substeps:</p>
<div class="note"><div class="noteHeader">Note</div><p>The algorithm assumes that invoking methods have checked for <a href="#readabilityState">readability state</a>. A <a href="#dfn-Blob"><code>Blob</code></a> in the <a href="#dfn-closedState"><code>CLOSED</code></a> state must not have a <a href="#readOperation">read operation</a> called on it.</p></div>
<ol>
<li><p>If the <a href="#synchronousFlag">synchronous flag</a> is set, follow the steps below: </p>
<ol><li><p>Let <var>bytes</var> be the byte sequence that results from reading a <a href="#chunk">chunk</a> from <var>b</var>. If <a href="#file-error-read">an error occurs</a> reading a <a href="#chunk">chunk</a> from <var>b</var>, return <var>s</var> with the error flag set, along with a <a href="#failureReason">failure reason</a>, and <a href="#terminate-an-algorithm">terminate this algorithm</a>. </p>
<div class="note"><div class="noteHeader">Note</div>
<p>Along with returning failure, the synchronous part of this algorithm must return the <a href="#failureReason">failure reason</a> that occurred for <a href="#throw-an-exception">throwing an exception</a> by synchronous methods that invoke this algorithm with the <a href="#synchronousFlag">synchronous flag</a> set. </p>
</div>
</li>
<li><p>If there are no errors, push <var>bytes</var> to <var>s</var>, and increment <var>s</var>'s <var>transmitted</var> [<a href="#Fetch">Fetch</a>] by the number of bytes in <var>bytes</var>. Reset <var>bytes</var> to the empty byte sequence and continue reading <a href="#chunk">chunk</a>s as above.</p></li>
<li><p>When all the bytes of <var>b</var> have been read into <var>s</var>, return <var>s</var> and <a href="#terminate-an-algorithm">terminate this algorithm</a>.</p></li>
</ol>
</li>
<li><p>Otherwise, the <a href="#synchronousFlag">synchronous flag</a> is unset. Return <var>s</var> and process the rest of this algorithm asynchronously.</p></li>
<li><p>Let <var>bytes</var> be the byte sequence that results from reading a <a href="#chunk">chunk</a> from <var>b</var>. If <a href="#file-error-read">an error occurs</a> reading a <a href="#chunk">chunk</a> from <var>b</var>, set the error flag on <var>s</var>, and <a href="#terminate-an-algorithm">terminate this algorithm</a> with a <a href="#failureReason">failure reason</a>.</p>
<div class="note"><div class="noteHeader">Note</div>
<p>The asynchronous part of this algorithm must signal the <a href="#failureReason">failure reason</a> that occurred for asynchronous error reporting by methods expecting <var>s</var> and which invoke this algorithm with the <a href="#synchronousFlag">synchronous flag</a> unset. </p>
</div></li>
<li><p>If no <a href="#file-error-read">error</a> occurs, push <var>bytes</var> to <var>s</var>, and increment <var>s</var>'s <var>transmitted</var> [<a href="#Fetch">Fetch</a>] by the number of bytes in <var>bytes</var>. Reset <var>bytes</var> to the empty byte sequence and continue reading <a href="#chunk">chunk</a>s as above. </p></li>
</ol>
<p>To perform an <dfn id="task-read-operation">annotated task read operation</dfn> on a <a href="#dfn-Blob">Blob</a> <var>b</var>, perform the steps below:</p>
<ol>
<li><p>Perform a <a href="#readOperation">read operation</a> on <var>b</var> with the <a href="#synchronousFlag">synchronous flag</a> unset, along with the additional steps below.</p></li>
<li><p>If the <a href="#readOperation">read operation</a> terminates with a <a href="#failureReason">failure reason</a>, <a href="#queue-a-task">queue a task</a> to <dfn id="process-read-error">process read error</dfn> with the <a href="#failureReason">failure reason</a> and terminate this algorithm.</p></li>
<li><p>When the first <a href="#chunk">chunk</a> is being pushed to the <a href="#dfn-body">body</a> <var>s</var> during the <a href="#readOperation">read operation</a>, <a href="#queue-a-task">queue a task</a> to <dfn id="process-read">process read</dfn>.</p></li>
<li><p>Once the <a href="#dfn-body">body</a> <var>s</var> from the <a href="#readOperation">read operation</a> has at least one <a href="#chunk">chunk</a> read into it, or there are no <a href="#chunk">chunk</a>s left to read from <var>b</var>, <a href="#queue-a-task">queue a task</a> to <dfn id="process-read-data">process read data</dfn>. Keep <a href="#queue-a-task">queuing tasks</a> to <a href="#process-read-data">process read data</a> for every <a href="#chunk">chunk</a> read or every 50ms, whichever is <em>least frequent</em>.</p></li>
<li><p>When all of the <a href="#chunk">chunk</a>s from <var>b</var> are read into the <a href="#dfn-body">body</a> <var>s</var> from the <a href="#readOperation">read operation</a>, <a href="#queue-a-task">queue a task</a> to <dfn id="process-read-EOF">process read EOF</dfn>.</p></li>
</ol>
</li>
</ol>
<p>Use the <a href="#fileReadingTaskSource">file reading task source</a> for all these tasks.</p>
</div>
<div id="blobreader-task-source" class="section">
<h3>8.2. The File Reading Task Source</h3>
<p>This specification defines a new generic <a href="#task-source">task source</a> called the <dfn id="fileReadingTaskSource">file reading task source</dfn>, which is used for all <a href="#queue-a-task">tasks that are queued</a> in this specification to read byte sequences associated with <a href="#dfn-Blob"><code>Blob</code></a> and <a href="#dfn-file"><code>File</code></a> objects. It is to be used for features that trigger in response to asynchronously reading binary data.</p>
</div>
<div id="APIASynch" class="section">
<h3>8.3 The FileReader API</h3>
<div class="block"><div class="blockTitleDiv"><span class="blockTitle">IDL</span></div><div class="blockContent"><pre class="code"><code class="idl-code">
[Constructor, Exposed=Window,Worker]
interface <dfn id="dfn-filereader">FileReader</dfn>: EventTarget {
<span class="comment">// async read methods</span>
void <a href="#dfn-readAsArrayBuffer" class="dfnref">readAsArrayBuffer</a>(<a href="#dfn-Blob" class="dfnref">Blob</a> <a href="#dfn-fileBlob" class="dfnref">blob</a>);
void <a href="#dfn-readAsText" class="dfnref">readAsText</a>(<a href="#dfn-Blob" class="dfnref">Blob</a> <a href="#dfn-fileBlob" class="dfnref">blob</a>, optional DOMString <a href="#dfn-label" class="dfnref">label</a>);
void <a href="#dfn-readAsDataURL" class="dfnref">readAsDataURL</a>(<a href="#dfn-Blob" class="dfnref">Blob</a> <a href="#dfn-fileBlob" class="dfnref">blob</a>);
void <a href="#dfn-abort" class="dfnref">abort</a>();
<span class="comment">// states</span>
const unsigned short <a href="#dfn-empty" class="dfnref">EMPTY</a> = 0;
const unsigned short <a href="#dfn-loading" class="dfnref">LOADING</a> = 1;
const unsigned short <a href="#dfn-done" class="dfnref">DONE</a> = 2;
readonly attribute unsigned short <a class="dfnref" href="#dfn-readyState">readyState</a>;
<span class="comment">// File or Blob data</span>
readonly attribute (DOMString or ArrayBuffer)? <a href="#dfn-result" class="dfnref">result</a>;
readonly attribute <a href="#dfn-domerror" class="dfnref">DOMError</a>? <a href="#dfn-error" class="dfnref">error</a>;
<span class="comment">// event handler attributes</span>
attribute <a href="http://www.w3.org/html/wg/drafts/html/master/webappapis.html#eventhandler">EventHandler</a> <a href="#dfn-onloadstart" class="dfnref">onloadstart</a>;