我用了之后,在后台一直取不到files,这个是什么原因呢。
easyUpload({ easyId: 'easy1', action: 'easyuploadHandler.ashx', type: 'post', accept: '.jpg,.png,.gif,.doc,.docx,.xls,.xlsx,.ppt,.pptx,.pdf,.zip,.rar,.mp3,.mp4,.wma,.avi,.txt', maxSize: 20, //最大20M maxCount: 1, //插件单次添加文件的最大数量,Number类型 multiple: false, ? //是否开启多文件上传,Boolean类型 name : 'file', //上传的文件字段名 dataFormat: 'formData', setRequestHeader: function(xhr) { xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded'); //xhr.setRequestHeader('content-type', 'multipart/form-data'); }, beforeUpload: function(file, data, args) { data.append('token', file.id); //上传方式为formData时 }, buildSendData: function(file) { var formData = new FormData(); // 发送格式为formData时 console.log(file.file.name); formData.append('name', file.file); //formData.set('name', file.file); formData.append('filename', file.file.name); console.log(formData.get('name')); //console.log(formData); return formData; // return file.base64; //发送格式为base64时 //return null; //发送空数据,用于测试 }, checkSuccessCode: function(xhr) { console.log(xhr); if (/error/.test(xhr.responseText.toLowerCase())) { //这里判断仅仅用于测试,具体看项目 return false; } else { return true; } } });回复
beforeUpload: function(file, data, args) { data.append('token', file.id); //上传方式为formData时 },
执行上传前的回调函数的时候如果file ,data 没有初始化且方法最开始执行会报错,每个人的引用方式不一样,因为我用到了file,直接把data 初始化 file初始化可以避免这种小错误。
有个问题,删除回调不管用,这问题可以在源码中修改,在源码中搜索'$(".btn--file")'和'$(".btn--item")'的两个事件,这俩分别是顶部多选的删除文件按钮,和每列中右上角的'X'按钮。然后在各自的事件中添加'that.configs.onRemove && that.configs.onRemove(delFiles);'即可。例如:
easyUploader.js 195行:
$(".btn--file").off("click").on("click", function() { var hasLoading = false; that.files.forEach(function(item) { if (item.checked && item.uploadStatus == 'loading') { hasLoading = true; } }); if (hasLoading) { var message = '正在上传,请稍后操作'; if (that.configs.showAlert) { alert(message); } that.configs.onAlert && that.configs.onAlert(message); } else { var newFiles = []; var delFiles = [] //新建一个删除文件的数组, that.ajax.isReady = false; that.files.forEach(function(item) { if (item.checked) { //判断当文件列表右侧多选为选中时,将该文件信息添加到删除数组中。 delFiles.push(item) } else { newFiles.push(item); } //这里是源文件的方法,将没有选中删除的文件放在这里。 }); that.files = newFiles; that.updateFilesDom(); that.ajax.isReady = true; that.configs.onRemove && that.configs.onRemove(delFiles); //执行删除回调 } });
assignObject: function(target, source1, source2) { target = source1; /* 简单复制地址,严谨来说应该是做深度拷贝处理 */ Object.keys(source2).forEach(function(key) { if (target[key] != undefined) { target[key] = source2[key]; } }); return target; },
这个地方
if (target[key] != undefined) { target[key] = source2[key]; }
在赋值的时候 把事件给搞掉了!
两个地方建议改下:
1.上传大文件会崩。
convertToBase: function(file) { // var reader, // isImg = this.checkImg(file); // if (typeof FileReader !== 'undefined') { // reader = new FileReader(); // } else { // if (window.FileReader) reader = new window.FileReader(); // } // reader.onload = (e) => { // var base = e.target.result; // if (isImg) { // this.compressImg(base, file); // } else { // this.formatFile(file, iconFile, base); // } // } // reader.readAsDataURL(file); var base = ""; this.formatFile(file, iconFile, base); },
convertToBase这个方法修改,去掉了加载文件的操作,全程好像也不需要用到,注释掉的为原来的代码。当然,图片不大要预览可以转一下。
2. 随着文件选择次数增加,每次添加文件会重复。
找了老半天,终于找到了问题所在。
$(".btn-select-file").off("click").on("click", function() { that.node.list = $(this).parent().siblings(".list"); console.info("chufa"); $(this).parent().children(".input-file").trigger("click").on("change", function(e) { that.fileObj = { fileList: e.target.files, isReady: true }; that.node.input = $(this); console.info("doUpdateFiles") that.updateFiles(); that.configs.onChange && that.configs.onChange(e.target.files); $(this).parent().children(".input-file").unbind("change"); }); });
这个文件选择点击事件中,在方法中加入
$(this).parent().children(".input-file").unbind("change");
否则在每次点击选择文件都会触发上次的onchange,所以文件重复添加了