下面我们了解一下,多图上传时,怎么实现预览、上传、删除等功能。下图是功能实现的预览效果,虽然样式有点丑,不过功能还是实现了。话不多说,直接看代码会更直观一些。
创新互联建站主要从事网页设计、PC网站建设(电脑版网站建设)、wap网站建设(手机版网站建设)、成都响应式网站建设公司、程序开发、网站优化、微网站、小程序设计等,凭借多年来在互联网的打拼,我们在互联网网站建设行业积累了丰富的网站设计、成都网站建设、网站设计、网络营销经验,集策划、开发、设计、营销、管理等多方位专业化运作于一体。
![jQuery实现input[type=file]多图预览上传删除等功能](/upload/otherpic51/17330.png)
首先定义一下基本格式,样式代码自行脑补:
接着定义看一下具体的js实现代码,我是基于JQ做的开发,方便DOM的操作。
1. 预览功能的实现
这里监听input[type=file]的change事件,在回调函数中,取到暂存区的文件e.target.files,通过遍历files 文件属性,使用FileReader 函数读取文件的值,然后使用append方法把图片放入指定盒子中。备注::FileReader是一种异步文件读取机制,结合input:file可以很方便的读取本地文件。
imgPreview: function () {
var that = this;
$('.upload-header').on('change', '#upload', function(e) {
var files = e.target.files;
if (files.length > 0) {
for (var i = 0; i < files.length; i++) {
var reader = new FileReader();
reader.onload = function () {
var text = `
删除
`
$('.img-box').append(text);
};
reader.readAsDataURL(files[i]);
that.filesList.push(files[i]);
};
};
})
}2. 图片删除功能
这里是使用on方法对预览的图片添加点击事件,这里使用on方法主要是因为on方法对后面添加的dom可以继续监听。当需要删除照片时,直接删除其图片的父节点及以下节点就可以了。此时记得把暂存区的文件filesList也要对应删除。
delImage: function () {
var that = this;
$('.img-box').on('click', '.del-img', function () {
var delStatus = confirm('确认这张图片删除吗?');
if (delStatus) {
var index = $(this).parent().index();
$(this).parent().remove();
that.filesList.splice(index, 1);
console.log('我删除啦', index);
};
});
}2. 图片批量上传功能
图片上传我定义了两个方法,第一方法是点击上传后,对所有文件进行遍历,依次调取上传的方法(因为用的七牛上传工具,只支持单个文件上传,所以采取了遍历的方式)。第二个方法使用了jq的ajax的post上传方式,使用FormData实例,添加file属性进行上传。因为是批量上传,所以我使用了定义临时变量flag 的方式来判断文件是否都上传完成,当flag的值与filesList的数量一致时,就可以判断所有上传完成。使用方法比较low,如果其他好的方法可以留言交流。
clickUpload: function () {
var that = this;
var filesList = this.filesList;
$('.btn').on('click', function() {
that.flag = 0;
if (filesList.length > 0) {
for (var i = 0; i < filesList.length; i++) {
that.upLoadMethod(filesList[i]);
}
};
})
},
upLoadMethod: function (file) {
var that = this;
var formData = new FormData();
formData.append('file', file);
$.ajax({
type: "post",
url: 'http://172.16.1.99:8703/file/upload',
data: formData,
mimeType: "multipart/form-data",
dataType: "json",
async: false,
cache: false, //上传文件不需要缓存
contentType: false, //需设置为false。因为是FormData对象,且已经声明了属性enctype="multipart/form-data"
processData: false, //需设置为false。因为data值是FormData对象,不需要对数据做处理
success: function (response) {
that.flag += 1;
if (that.flag === that.filesList.length) {
console.log('我上传完成了');
};
},
error: function (err) {
console.log('上传失败');
}
});
},以下是完整的js代码,供参考:
基本
文件
流程
错误
SQL
调试
- 请求信息 : 2026-06-08 08:46:56 HTTP/1.1 GET : /article/gcjiep.html
- 运行时间 : 0.9232s ( Load:0.0075s Init:0.2293s Exec:0.6774s Template:0.0090s )
- 吞吐率 : 1.08req/s
- 内存开销 : 2,235.28 kb
- 查询信息 : 12 queries 5 writes
- 文件加载 : 36
- 缓存信息 : 0 gets 0 writes
- 配置加载 : 130
- 会话信息 : SESSION_ID=ups0109t3mqo6htu3sk2ptu554
- /www/wwwroot/tsicrk.com/index.php ( 1.09 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/ThinkPHP.php ( 4.61 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Think.class.php ( 12.26 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Storage.class.php ( 1.37 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Storage/Driver/File.class.php ( 3.52 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Mode/common.php ( 2.82 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Common/functions.php ( 53.56 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Hook.class.php ( 4.01 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/App.class.php ( 13.49 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Dispatcher.class.php ( 14.79 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Route.class.php ( 13.36 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Controller.class.php ( 11.23 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/View.class.php ( 7.59 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Behavior/BuildLiteBehavior.class.php ( 3.68 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Behavior/ParseTemplateBehavior.class.php ( 3.88 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Behavior/ContentReplaceBehavior.class.php ( 1.91 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Conf/convention.php ( 11.15 KB )
- /www/wwwroot/tsicrk.com/App/Common/Conf/config.php ( 2.14 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Lang/zh-cn.php ( 2.55 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Conf/debug.php ( 1.49 KB )
- /www/wwwroot/tsicrk.com/App/Home/Conf/config.php ( 0.31 KB )
- /www/wwwroot/tsicrk.com/App/Home/Common/function.php ( 3.33 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Behavior/ReadHtmlCacheBehavior.class.php ( 5.62 KB )
- /www/wwwroot/tsicrk.com/App/Home/Controller/ArticleController.class.php ( 6.02 KB )
- /www/wwwroot/tsicrk.com/App/Home/Controller/CommController.class.php ( 1.60 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Model.class.php ( 60.11 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Db.class.php ( 32.43 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Db/Driver/Pdo.class.php ( 16.74 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Cache.class.php ( 3.83 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Cache/Driver/File.class.php ( 5.87 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Template.class.php ( 28.16 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Template/TagLib/Cx.class.php ( 22.40 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Template/TagLib.class.php ( 9.16 KB )
- /www/wwwroot/tsicrk.com/App/Runtime/Cache/Home/7540f392f42b28b481b30614275e4e55.php ( 17.71 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Behavior/WriteHtmlCacheBehavior.class.php ( 0.97 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Behavior/ShowPageTraceBehavior.class.php ( 5.24 KB )
- [ app_init ] --START--
- Run Behavior\BuildLiteBehavior [ RunTime:0.000005s ]
- [ app_init ] --END-- [ RunTime:0.000031s ]
- [ app_begin ] --START--
- Run Behavior\ReadHtmlCacheBehavior [ RunTime:0.000291s ]
- [ app_begin ] --END-- [ RunTime:0.000315s ]
- [ view_parse ] --START--
- [ template_filter ] --START--
- Run Behavior\ContentReplaceBehavior [ RunTime:0.000053s ]
- [ template_filter ] --END-- [ RunTime:0.000073s ]
- Run Behavior\ParseTemplateBehavior [ RunTime:0.006199s ]
- [ view_parse ] --END-- [ RunTime:0.006224s ]
- [ view_filter ] --START--
- Run Behavior\WriteHtmlCacheBehavior [ RunTime:0.000156s ]
- [ view_filter ] --END-- [ RunTime:0.000171s ]
- [ app_end ] --START--
- 1064:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') LIMIT 1' at line 1
[ SQL语句 ] : SELECT `id`,`pid`,`navname` FROM `cx_nav` WHERE ( id= ) LIMIT 1
- 1064:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') LIMIT 1' at line 1
[ SQL语句 ] : SELECT `id`,`navname` FROM `cx_nav` WHERE ( id= ) LIMIT 1
- 1064:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 1
[ SQL语句 ] : SELECT `id`,`navname` FROM `cx_nav` WHERE ( pid= )
- [8] Undefined index: pid /www/wwwroot/tsicrk.com/App/Home/Controller/ArticleController.class.php 第 47 行.
- [8] Undefined index: db_host /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Db.class.php 第 120 行.
- [8] Undefined index: db_port /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Db.class.php 第 121 行.
- [8] Undefined index: db_name /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Db.class.php 第 122 行.

0.9232s
