HTML5的input:file上传以及类型控制以HTML5的⽂件上传API
如下demo代码在.html⽂件打开即可:
!DOCTYPE html>
<html lang="zh_cn">
<head>
<meta charset="UTF-8">
<title>HTML5⽂件上传FileReader API</title>
</head>
<body>
<!--⼀个能上传多媒体⽂件的表单-->
<input type="file" id="upload-file" multiple />
<!--显⽰图⽚的地⽅-->
<div id="destination"></div>
<script>
var file;
var destination = ElementById('destination');
destination.innerHTML = '';//每次清空内容
// 循环⽤户多选的⽂件
for(var x = 0, xlen = this.files.length; x < xlen; x++) {
file = this.files[x];
console.log(file);
pe.indexOf('image') != -1) { // ⾮常简单的交验,判断⽂件是否是图⽚
var reader = new FileReader();//⽂件预览对象
var img = new Image();//创键预览图⽚
img.src = sult; //把预览图⽚的src设置为sult属性。返回预览⽂件的⼆进制内存数据 destination.appendChild(img);//在把每张图⽚,放到预览区域
};
}
}
});
</script>
</body>
</html>
⼀、input:file属性
属性值有以下⼏个⽐较常⽤:
accept:表⽰可以选择的⽂件MIME类型,多个MIME类型⽤英⽂逗号分开,常⽤的MIME类型见下表。multiple:是否可以选择多个⽂件,多个⽂件时其value值为第⼀个⽂件的虚拟路径。
1、accept
只能选择png和gif图⽚
<input id="fileId1" type="file" accept="image/png,image/gif" name="file" />
2、multiple
多选⽂件上传
inputtypefile样式<input id="fileId2" type="file" multiple="multiple" name="file" />
3、常⽤MIME类型
后缀名 MIME名称
*.3gpp audio/3gpp, video/3gpp
*.ac3 audio/ac3
*.asf allpication/vnd.ms-asf
*.au audio/basic
*.css text/css
*.csv text/csv
*.doc application/msword
*.dot application/msword
*.dtd application/xml-dtd
*.dwg image/vnd.dwg
*.dxf image/vnd.dxf
*.gif image/gif
*.htm text/html
*.html text/html
*.jp2 image/jp2
*.jpe image/jpeg
*.jpeg image/jpeg
*.jpg image/jpeg
*.js text/javascript, application/javascript
*.json application/json
*.mp2 audio/mpeg, video/mpeg
*.mp3 audio/mpeg
*.mp4 audio/mp4, video/mp4
*.mpeg video/mpeg
*.mpg video/mpeg
*.mpp application/vnd.ms-project
*.ogg application/ogg, audio/ogg
*.pdf application/pdf
*.png image/png
*.pot application/vnd.ms-powerpoint
*.pps application/vnd.ms-powerpoint
*.ppt application/vnd.ms-powerpoint
*.rtf application/rtf, text/rtf
*.svf image/vnd.svf
*.tif image/tiff
*.tiff image/tiff
*.txt text/plain
*.wdb application/vnd.ms-works
*.wps application/vnd.ms-works
*.xhtml application/xhtml+xml
*.xlc application/vnd.ms-excel
*.xlm application/vnd.ms-excel
*.xls application/vnd.ms-excel
*.xlt application/vnd.ms-excel
*.xlw application/vnd.ms-excel
*.xml text/xml, application/xml
*.zip aplication/zip
*.xlsx application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
⼆、
三、AJAX上传⽂件
在说到ajax上传⽂件。ajax上传的时候,需要获得input:file选择的⽂件(可能为多个⽂件),获取其⽂件列表为:// input标签的files属性
document.querySelector("#fileId").files
// 返回的是⼀个⽂件列表数组
获得的⽂件列表,然后遍历插⼊到表单数据当中。即:
// 获得上传⽂件DOM对象
var oFiles = document.querySelector("#fileId");
// 实例化⼀个表单数据对象
var formData = new FormData();
// 遍历图⽚⽂件列表,插⼊到表单数据中
for (var i = 0, file; file = oFiles[i]; i++) {
// ⽂件名称,⽂件对象
formData.append(file.name, file);
}
获得表单数据之后,就可以⽤ajax的POST上传。
/
/ 实例化⼀个AJAX对象
var xhr = new XMLHttpRequest();
alert("上传成功!");
}
xhr.open("POST", "upload.php", true);
// 发送表单数据
xhr.send(formData);
上传到服务器之后,获取到⽂件列表为:
Array
(
[jpg_jpg] => Array
(
[name] => jpg.jpg
[type] => image/jpeg
[tmp_name] => D:\xampp\p
[error] => 0
[size] => 133363
)
[png_png] => Array
(
[name] => png.png
[type] => image/png
[tmp_name] => D:\xampp\p
[error] => 0
[size] => 1214628
)
)
在服务端循环遍历这个数组就可以上传⽂件了。
// var formData = new FormData();
// formData.append("realName", this.nameText || 'a');
// formData.append("personalImg", event.files[0]);
// formData.append("idCard", this.cardNum || 'b');
/
/ var request = new XMLHttpRequest();
// request.open("POST", "xxx/fileUpoad/contract");
// request.setRequestHeader("Authorization", "Bearer eyJhbGciOiJSUzI1Ni6w");// request.send(formData);
H5⽂件上传
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论