JavaScript实现form表单的多⽂件上传
form表单的多⽂件上传,具体内容如下
formData对象可以使⽤⼀系列的键值对来模拟⼀个完整的表单,然后使⽤Ajax来发送这个表单
使⽤<form>表单初始化FormData对象的⽅式上传⽂件
<!--⽂件上传-->
<form id="uploadForm" enctype="multipart/form-data">
<div class="row" >
<div class="form-group col-md-12" id="file">
<input type="hidden" name="_csrf-application"
value="<?= $csrf ?>">
<div class="form-group field-uploadform-excelfiles" >
<label class="control-label btn btn-primary"
for="uploadform-excelfiles">选择⽂件</label>
<input type="file" id="uploadform-excelfiles" name="UploadForm[excelFiles][]"
multiple class="attachment-upload" accept=".xlsx">
<input type="button" id="fileUpload" value="上传⽂件" class="btn btn-success" >
<div class="help-block"></div>
<div id="fileName"></div>
</div>
</div>
</div>
<table role="presentation" class="table"><tbody id="files"></tbody></table>
</form>
注意:
1. 使⽤formData对象进⾏表单上传必须要为form添加enctype="multipart/form-data"属性
2. 使⽤formData对象进⾏表单上传必须要对其开始填⼊的值按照name属性放⼊该对象中,不能开始使⽤action上传,后⾯使⽤formData进⾏上传,这样会导致上传数据出现错误
获取change事件改变的⽂件
var fileList;
var allFile = [];
//FormData对象初始化
var form = ElementById("upload-form");
var formData = new FormData(form);
$("#uploadform-excelfiles").on('change', function (e) {
//获取表单数据并传⼊formData中
var norm = $("#norm").val();
var major = $("#major").val();
var type = $("#type").val();
formData.set("norm",norm);
formData.set("major",major);
formData.set("type",type);
var fileError = 0;
fileList = e.currentTarget.files;
$.each(fileList, function (index, item) {
var fileName = item.name;
var fileEnd = fileName.substring(fileName.indexOf("."));
//上传⽂件格式判断
if (fileEnd == ".xlsx") {
allFile.push(item);
$('#files').append( '<tr >' +
'<td>'+fileName+'</td>' +
'<td>'+(item.size / 1024).toFixed(2)+'K</td>' +
'<td><input type="button" class="btn btn-danger delete" value="删除"></td>' +
'</tr>');
//追加⽂件
formData.append('UploadForm[excelFiles][]',item);
} else {
fileError++;
}
});
if (fileError > 0) {
alert("只能上传 “.xlsx” 格式的⽂件!");
return;
}
var fileCount = $('#files').find('tr').length;
$('#fileName').html('共上传 ' + fileCount + ' 个⽂件');
});
删除按钮事件
$('#files').on('click','.delete',function (e) {
var saveFile = [];
var norm = $("#norm").val();
var major = $("#major").val();
var type = $("#type").val();
var deleteName = e.target.parentNode.Content;
var deleteIndex;
//将不删除的放⼊数组中
$.each(allFile,function (index, item) {
if(item.name == deleteName){
deleteIndex = index;
}else {
saveFile.push(item);
}
});
allFile.splice(deleteIndex,1);
//表单数据重置
formData.set("norm",norm);
formData.set("major",major);
formData.set("type",type);
formData.delete('UploadForm[excelFiles][]');
//将不删除的数组追加的formData中
$.each(saveFile,function (index, item) {
formData.append('UploadForm[excelFiles][]',item);
});
e.target.ve();
var fileCount = $('#files').find('tr').length;
$('#fileName').html('共上传 ' + fileCount + ' 个⽂件');
});
⽂件上传事件
$("#fileUpload").on('click',function () {
var len = All('UploadForm[excelFiles][]').length;
$("#overlay").show();
if(len > 1){
var deleteBtn = $(".delete");
//通过ajax进⾏上传
$.ajax({
url: '/finalize/upload',
type: 'POST',
cache: false,
data: formData,
processData: false,
contentType: false
}).done(function(res) {
de == 'ok'){
//进度条设置
var value = 0;
var timer2 = setInterval(function () {
value ++;
$("#progress").css('width', value + "%");
if (value == 120) {inputtypefile不上传文件
clearInterval(timer2);
$("#overlay").hide();
alert("⽂件上传成功!");
}
}, 50);
//删除对应按钮
$("#fileUpload").css("display","none");
$.each(deleteBtn,function (index, item) {
$(item).css("display","none");
});
$('#files').append('<tr><td><td><td><a type="button" class="btn btn-success pull-right" id="fileDown" href="/finalize/get-file?id=' + res.data.id + '" >⽂件下载</a></td></tr>') }
}).fail(function(res) {
alert("⽂件上传失败:" + res.msg);
});
}else {
alert("请选择需要上传的⽂件!");
}
});
更多精彩内容请参考专题,和进⾏学习。
以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论