编程 HTML5的 input:file上传类型控制

2024-11-19 07:29:28 +0800 CST views 2693

一、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

多文件上传

<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();xhr.onload = function() { alert("上传成功!");}xhr.open("POST", "upload.php", true);// 发送表单数据xhr.send(formData);

上传到服务器之后,获取到文件列表为:

Array( [jpg_jpg] => Array ( [name] => jpg.jpg [type] => image/jpeg [tmp_name] => D:\xampp\tmp\phpA595.tmp [error] => 0 [size] => 133363 ) [png_png] => Array ( [name] => png.png [type] => image/png [tmp_name] => D:\xampp\tmp\phpA5A6.tmp [error] => 0 [size] => 1214628 ))

在服务端循环遍历这个数组就可以上传文件了。

复制全文 生成海报 HTML 文件上传 AJAX 前端开发 Web技术

推荐文章

Vue3中如何进行错误处理?
2024-11-18 05:17:47 +0800 CST
php 连接mssql数据库
2024-11-17 05:01:41 +0800 CST
回到上次阅读位置技术实践
2025-04-19 09:47:31 +0800 CST
Vue3中如何处理组件间的动画?
2024-11-17 04:54:49 +0800 CST
快手小程序商城系统
2024-11-25 13:39:46 +0800 CST
百度开源压测工具 dperf
2024-11-18 16:50:58 +0800 CST
随机分数html
2025-01-25 10:56:34 +0800 CST
mendeley2 一个Python管理文献的库
2024-11-19 02:56:20 +0800 CST
Vue中的表单处理有哪几种方式?
2024-11-18 01:32:42 +0800 CST
20个超实用的CSS动画库
2024-11-18 07:23:12 +0800 CST
Vue中如何使用API发送异步请求?
2024-11-19 10:04:27 +0800 CST
Manticore Search:高性能的搜索引擎
2024-11-19 03:43:32 +0800 CST
js迭代器
2024-11-19 07:49:47 +0800 CST
程序员茄子在线接单