--- title: File slug: Web/API/File tags: - API - File API - Interface - Reference - Web translation_of: Web/API/File ---
{{APIRef}}

File cung cấp thông tin về các tệp và cho phép Javascript truy cập nội dung của chúng.

File thường được lấy từ một {{DOMxRef("FileList")}} đối tượng trả về như là kết quả của người dùng chọn files sử dụng phần tử {{HTMLElement("input")}}, từ thao tác kéo và thả đối tượng {{DOMxRef("DataTransfer")}}, hoặc là từ mozGetAsFile() API trên một {{DOMxRef("HTMLCanvasElement")}}.

Đối tượng file là một loại {{DOMxRef("Blob")}}, và có thể sử dụng trong mọi hoàn cảnh mà Blob có thể sử dụng. Cụ thể, {{DOMxRef("FileReader")}}, {{DOMxRef("URL.createObjectURL()")}}, {{DOMxRef("ImageBitmapFactories.createImageBitmap()", "createImageBitmap()")}}, và {{DOMxRef("XMLHttpRequest", "", "send()")}} chấp nhận cả Blobs và Files.

Đọc Using files from web applications để biết thêm thông tin và ví dụ.

{{InheritanceDiagram}}

Constructor

{{DOMxRef("File.File", "File()")}}
Trả về một constructed File mới.

Thuộc tính

{{DOMxRef("File.lastModified")}} {{ReadOnlyInline}}
Trả về thời gian sửa đổi cuối cùng của file, tính bằng mili giây kể từ kỉ nguyên UNIX (January 1st, 1970 at Midnight).
{{DOMxRef("File.lastModifiedDate")}} {{Deprecated_Inline}} {{ReadOnlyInline}}
Returns the last modified {{JSxRef("Date")}} of the file referenced by the File object.
{{DOMxRef("File.name")}}{{ReadOnlyInline}}
Trả về tên của tệp referenced by the File object.
{{DOMxRef("File.webkitRelativePath")}} {{Non-standard_Inline}} {{ReadOnlyInline}}
Trả về đường dẫn (URL) của {{DOMxRef("File")}} có liên quan.

File bổ sung {{DOMxRef("Blob")}}, Vì vậy, nó cũng có các thuộc tính có sẵn:

{{DOMxRef("File.size")}} {{ReadOnlyInline}}
Trả về kích thước của file (tính bằng bytes)
{{DOMxRef("File.type")}} {{ReadOnlyInline}}
Trả về thể loại MIME của file file.

Phương thức

File không có phương thức nào, nhưng nó thừa kế từ {{DOMxRef("Blob")}}:

{{DOMxRef("Blob.slice()", "Blob.slice([start[, end[, contentType]]])")}}
Trả về một đối tượng Blob mới có nội dung trong phạm vi bytes chỉ định của Blob
{{DOMxRef("Blob.stream()")}}
Chuyển đổi file thành {{DOMxRef("ReadableStream")}} vì vậy có thể sử dụng để đọc nội dung file.
{{DOMxRef("Blob.text()")}}
Chuyển đổi file thành stream và đọc nó để hoàn thành. Nó trả về một promise có thể giải quyết với {{DOMxRef("USVString")}} (text).
{{DOMxRef("Blob.arrayBuffer()")}}
Chuyển đổi file thành stream và đọc nó để hoàn thành. Nó trả về một promise có thể giải quyết với {{DOMxRef("ArrayBuffer")}}.

Specifications

Specification Status Comment
{{SpecName('File API', "#file-section", "The File interface")}} {{Spec2('File API')}} Initial definition.

Browser compatibility

{{Compat("api.File")}}

Xem thêm