| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/ko/docs/Web/HTML/Reference/Elements/input/file | [Back] [Original] |
Get to know MDN better
This page was translated from English by the community. Learn more and join the MDN Web Docs community.
This feature is well established and works across many devices and browser versions. Its been available across browsers since 2015 7.
<label for="avatar">Choose a profile picture:</label>
<input type="file" id="avatar" name="avatar" accept="image/png, image/jpeg" />
label {
display: block;
font:
1rem "Fira Sans",
sans-serif;
}
input,
label {
margin: 0.4rem 0;
}
DOMString. |
|
change,
input
|
|
|
|
required |
accept,
capture,
files,
multiple
|
|
| IDL | files, value |
| DOM | HTMLInputElement |
select()
|
value DOMString . value , HTMLInputElement.files .
:
"") . 2. , C:\fakepath\ . <input> , file .
accept |
|
capture |
|
files |
FileList |
multiple |
acceptaccept , . , .
, Microsoft Word , Word <input> .
<input
type="file"
id="docpicker"
accept=".doc,.docx,application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document" />
captureaccept , capture . user ( ) , environment . capture . .
:
capture , .
filesmultiplemultiple .
, . , .
webkitdirectory |
. multiple |
webkitdirectory webkitdirectory . HTMLInputElement.webkitdirectory .
:
webkitdirectory WebKit , Microsoft Edge Firefox(50 ) . , , .
file <input> . .
.jpg, .pdf, .doc.audio/* " " .video/* " " .image/* " " .accept . . , PDF .
<input type="file" accept="image/*,.pdf" />
<form method="post" enctype="multipart/form-data">
<div>
<label for="file">Choose file to upload</label>
<input type="file" id="file" name="file" multiple />
</div>
<div>
<button>Submit</button>
</div>
</form>
div {
margin-bottom: 10px;
}
.
, .
multiple . (Shift, Ctrl ) . <input> multiple .
HTMLInputElement.files (File) FileList . FileList , length .
File .
name.
lastModified. UNIX (1970 1 1 ) .
lastModifiedDate Date . . lastModified .
size.
typeMIME .
webkitRelativePath webkitdirectory , . .
Note: You can set as well as get the value of HTMLInputElement.files in all modern browsers; this was most recently added to Firefox, in version 57 (see [Firefox bug 1384030](https://bugzil.la/1384030)).
accept , MIME . .
accept="image/png" or accept=".png" PNG .accept="image/png, image/jpeg" accept=".png, .jpg, .jpeg" PNG JPEG .accept="image/*" image/* MIME . , .accept=".doc,.docx,.xml,application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document" MS Word ..
<form method="post" enctype="multipart/form-data">
<div>
<label for="profile_pic">Choose file to upload</label>
<input
type="file"
id="profile_pic"
name="profile_pic"
accept=".jpg, .jpeg, .png" />
</div>
<div>
<button>Submit</button>
</div>
</form>
div {
margin-bottom: 10px;
}
.
, accept . ( )
[Screenshot of a macOS file picker dialog. Files other than JPEG are grayed-out and unselectable.]
accept , . ( ) accept , .
, accept .
. .
const input = document.querySelector("input[type=file]");
input.value = "foo";
. HTMLInputElement.files , .
: GitHub . file-example.html ( ). JavaScript CSS .
HTML .
<form method="post" enctype="multipart/form-data">
<div>
<label for="image_uploads">Choose images to upload (PNG, JPG)</label>
<input
type="file"
id="image_uploads"
name="image_uploads"
accept=".jpg, .jpeg, .png"
multiple />
</div>
<div class="preview">
<p>No files currently selected for upload</p>
</div>
<div>
<button>Submit</button>
</div>
</form>
html {
font-family: sans-serif;
}
form {
width: 580px;
background: #ccc;
margin: 0 auto;
padding: 20px;
border: 1px solid black;
}
form ol {
padding-left: 0;
}
form li,
div > p {
background: #eee;
display: flex;
justify-content: space-between;
margin-bottom: 10px;
list-style-type: none;
border: 1px solid black;
}
form img {
height: 64px;
order: 1;
}
form p {
line-height: 32px;
padding-left: 10px;
}
form label,
form button {
background-color: #7f9ccb;
padding: 5px 10px;
border-radius: 5px;
border: 1px ridge black;
font-size: 0.8rem;
height: auto;
}
form label:hover,
form button:hover {
background-color: #2d5ba3;
color: white;
}
form label:active,
form button:active {
background-color: #0d3f8f;
color: white;
}
.
JavaScript .
, .preview <div> . , <input> . , . <input> <label> , <input> , .
var input = document.querySelector("input");
var preview = document.querySelector(".preview");
input.style.opacity = 0;
( , ) . updateImageDisplay() .
input.addEventListener("change", updateImageDisplay);
updateImageDisplay() .
while <div> .
FileList curFiles .
curFiles.length 0 . , <div> .
, , <div> . :
validFileType() , accept .
,
<div> . file.name file.size . returnFileSize() /KB/MB . ( )URL.createObjectURL(curFiles[i]) , <img> src , ..
function updateImageDisplay() {
while (preview.firstChild) {
preview.removeChild(preview.firstChild);
}
const curFiles = input.files;
if (curFiles.length === 0) {
const para = document.createElement("p");
para.textContent = "No files currently selected for upload";
preview.appendChild(para);
} else {
const list = document.createElement("ol");
preview.appendChild(list);
for (const file of curFiles) {
const listItem = document.createElement("li");
const para = document.createElement("p");
if (validFileType(file)) {
para.textContent = `File name ${file.name}, file size ${returnFileSize(
file.size,
)}.`;
const image = document.createElement("img");
image.src = URL.createObjectURL(file);
listItem.appendChild(image);
listItem.appendChild(para);
} else {
para.textContent = `File name ${file.name}: Not a valid file type. Update your selection.`;
listItem.appendChild(para);
}
list.appendChild(listItem);
}
}
}
validFileType() File , type fileTypes . Array.prototype.includes() type true, false .
// https://developer.mozilla.org/ko/docs/Web/Media/Formats/Image_types
const fileTypes = [
"image/apng",
"image/bmp",
"image/gif",
"image/jpeg",
"image/pjpeg",
"image/png",
"image/svg+xml",
"image/tiff",
"image/webp",
"image/x-icon",
];
function validFileType(file) {
return fileTypes.includes(file.type);
}
returnFileSize() ( size , ) , /KB/MB .
function returnFileSize(number) {
if (number < 1024) {
return number + "bytes";
} else if (number >= 1024 && number < 1048576) {
return (number / 1024).toFixed(1) + "KB";
} else if (number >= 1048576) {
return (number / 1048576).toFixed(1) + "MB";
}
}
. .
| Specification |
|---|
| HTML # file-upload-state-(type=file) |
<input type="file"> File API .This page was last modified on 2026 4 15 by MDN contributors.
<a><abbr><acronym><address><area><article><aside><audio><b><base><bdi><bdo><big><blockquote><body><br><button><canvas><caption><center><cite><code><col><colgroup><data><datalist><dd><del><details><dfn><dialog><dir><div><dl><dt><em><embed><fencedframe><fieldset><figcaption><figure><font><footer><form><frame><frameset><geolocation><h1><head><header><hgroup><hr><html><i><iframe><img><input><ins><kbd><label><legend><li><link><main><map><mark><marquee><menu><meta><meter><nav><nobr><noembed><noframes><noscript><object><ol><optgroup><option><output><p><param><picture><plaintext><pre><progress><q><rb><rp><rt><rtc><ruby><s><samp><script><search><section><select><selectedcontent><slot><small><source><span><strike><strong><style><sub><summary><sup><table><tbody><td><template><textarea><tfoot><th><thead><time><title><tr><track><tt><u><ul><var><video><wbr><xmp><input> types<input type="button"><input type="checkbox"><input type="color"><input type="date"><input type="datetime-local"><input type="email"><input type="file"><input type="hidden"><input type="image"><input type="month"><input type="number"><input type="password"><input type="radio"><input type="range"><input type="reset"><input type="search"><input type="submit"><input type="tel"><input type="text"><input type="time"><input type="url"><input type="week"><script type>Your blueprint for a better internet.
Portions of this content are 19982026 by individual mozilla.org contributors. Content available under a Creative Commons license.
| Web Proxy Viewer | New URL | Original Page |