aboutsummaryrefslogtreecommitdiff
path: root/files/it/web/api/file/index.html
blob: 1d1941d4828403926f6e1c996e955f7cd5ad7768 (plain)
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
---
title: File
slug: Web/API/File
tags:
  - API
  - File API
  - Interface
  - NeedsTranslation
  - Reference
  - TopicStub
  - Web
translation_of: Web/API/File
---
<div>{{APIRef}}</div>

<p>The <strong><code>File</code></strong> interface provides information about files and allows JavaScript in a web page to access their content.</p>

<p><code>File</code> objects are generally retrieved from a {{DOMxRef("FileList")}} object returned as a result of a user selecting files using the {{HTMLElement("input")}} element, from a drag and drop operation's {{DOMxRef("DataTransfer")}} object, or from the <code>mozGetAsFile()</code> API on an {{DOMxRef("HTMLCanvasElement")}}.</p>

<p>A <code>File</code> object is a specific kind of a {{DOMxRef("Blob")}}, and can be used in any context that a Blob can. In particular, {{DOMxRef("FileReader")}}, {{DOMxRef("URL.createObjectURL()")}}, {{DOMxRef("ImageBitmapFactories.createImageBitmap()", "createImageBitmap()")}}, and {{DOMxRef("XMLHttpRequest", "", "send()")}} accept both <code>Blob</code>s and <code>File</code>s.</p>

<p>See <a href="/en-US/docs/Using_files_from_web_applications">Using files from web applications</a> for more information and examples.</p>

<p>{{InheritanceDiagram}}</p>

<h2 id="Constructor">Constructor</h2>

<dl>
 <dt>{{DOMxRef("File.File", "File()")}}</dt>
 <dd>Returns a newly constructed <code>File</code>.</dd>
</dl>

<h2 id="Properties">Properties</h2>

<dl>
 <dt>{{DOMxRef("File.lastModified")}} {{ReadOnlyInline}}</dt>
 <dd>Returns the last modified time of the file, in millisecond since the UNIX epoch (January 1st, 1970 at Midnight).</dd>
 <dt>{{DOMxRef("File.lastModifiedDate")}} {{Deprecated_Inline}} {{ReadOnlyInline}}</dt>
 <dd>Returns the last modified {{JSxRef("Date")}} of the file referenced by the <code>File</code> object.</dd>
 <dt>{{DOMxRef("File.name")}}{{ReadOnlyInline}}</dt>
 <dd>Returns the name of the file referenced by the <code>File</code> object.</dd>
 <dt>{{DOMxRef("File.webkitRelativePath")}} {{Non-standard_Inline}} {{ReadOnlyInline}}</dt>
 <dd>Returns the path the URL of the {{DOMxRef("File")}} is relative to.</dd>
</dl>

<p><code>File</code> implements {{DOMxRef("Blob")}}, so it also has the following properties available to it:</p>

<dl>
 <dt>{{DOMxRef("File.size")}} {{ReadOnlyInline}}</dt>
 <dd>Returns the size of the file in bytes.</dd>
 <dt>{{DOMxRef("File.type")}} {{ReadOnlyInline}}</dt>
 <dd>Returns the <a href="/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types">MIME </a>type of the file.</dd>
</dl>

<h2 id="Methods">Methods</h2>

<p><em>The <code>File</code> interface doesn't define any methods, but inherits methods from the {{DOMxRef("Blob")}} interface:</em></p>

<dl>
 <dt>{{DOMxRef("Blob.slice()", "Blob.slice([start[, end[, contentType]]])")}}</dt>
 <dd>Returns a new <code>Blob</code> object containing the data in the specified range of bytes of the source <code>Blob</code>.</dd>
 <dt>{{DOMxRef("Blob.stream()")}}</dt>
 <dd>Transforms the <code>File</code> into a {{DOMxRef("ReadableStream")}} that can be used to read the <code>File</code> contents.</dd>
 <dt>{{DOMxRef("Blob.text()")}}</dt>
 <dd>Transforms the <code>File</code> into a stream and reads it to completion. It returns a promise that resolves with a {{DOMxRef("USVString")}} (text).</dd>
 <dt>{{DOMxRef("Blob.arrayBuffer()")}}</dt>
 <dd>Transforms the <code>File</code> into a stream and reads it to completion. It returns a promise that resolves with an {{DOMxRef("ArrayBuffer")}}.</dd>
</dl>

<h2 id="Specifications">Specifications</h2>

<table class="standard-table">
 <thead>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>{{SpecName('File API', "#file-section", "The <code>File</code> interface")}}</td>
   <td>{{Spec2('File API')}}</td>
   <td>Initial definition.</td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility">Browser compatibility</h2>



<p>{{Compat("api.File")}}</p>

<h2 id="See_also">See also</h2>

<ul>
 <li><a href="/en-US/docs/Web/API/File/Using_files_from_web_applications">Using files from web applications</a></li>
 <li>{{DOMxRef("FileReader")}}</li>
 <li><a href="/en-US/docs/Extensions/Using_the_DOM_File_API_in_chrome_code">Using the DOM File API in chrome code</a> (for privileged code running in Gecko, such as Firefox add-ons)</li>
</ul>