input 实现一次性上传文件
在实际项目中可能会用到,上传多个文件请求一次接口,因此,主要代码
$('#tabList').on('click','.resetWorkStatus',function(){ var that = $(this); var curExamId = that.attr('data-value'); that.prev().addClass('selectWorkStatus'); layer.open({ text:"本地上传", closeBtn:0, btn:['上传','取消'], content:'<div style="text-align:center;" id="cont"><input type="file" class="" id="file" multiple="" value="选择文件"></div>' ,success:function(){ console.log('成功'); }, yes:function(){ //console.log('上传'); var formData = new FormData(); console.log($("#file")); var files = $("#file")[0].files; for(var i =0;i<files.length;i++){ //var name = files[i].name; var file = files[i]; formData.append("files", file); //formData.append("name" + i, name); } formData.append("id", curExamId); console.log(formData); $('.layui-layer-btn0,.layui-layer-btn1').hide(); $('.layui-layer-content').text('答题卡上传中,请耐心等待'); $.ajax({ url: "/exam/smallExam/uploadAnswerCardLocal", type: 'POST', data: formData, processData: false, // ⑧告诉jQuery不要去处理发送的数据 contentType: false, // ⑨告诉jQuery不要去设置Content-Type请求头 success: function (res) { console.log(res); }, error : function (responseStr) { } }); } }) })