开发室,往往遇到很多无聊的bug
例如,使用 ajaxFileUpload 上传时,只能上传一次!关于这个问题,我也是搞了好久!
方法一:
借助一个点击事件来实现同个 input 多次上传,直接上代码
html 代码
<input type="file" name="fileField" id="fileField">
<button type="button" id="cert_upload">上传</button>
js 代码
var btn = document.getElementById('cert_upload');
var file_path = '';
btn.onclick = function () {
var _fileField = $("#fileField").val();
if (_fileField.length < 1) {
alert("请选择上传的 cert");
return;
}
$.ajaxFileUpload({
url: '/Wechat/uploadCert/sid/{$sid}',//处理证书的脚本
secureuri: false,
fileElementId: 'fileField',//file控件id
dataType: 'json',
success: function (data) {
if (data.status != 1) {
alert(data.errorMessage);
} else {
file_path = data.file_path;
alert('上传成功');
}
},
error: function (data, status, e) {
// console.log(data)
}
});
};
方法二:
有时候,不想借助按钮上传,想直接选择文件之后自动上传, 我是通过修改 ajaxfileupload 的源码来实现的
源码修改前
var newElement = jQuery(oldElement).clone();
jQuery(oldElement).attr('id', fileId);
jQuery(oldElement).before(newElement);
jQuery(oldElement).appendTo(form);
源码修改后
var newElement = jQuery(oldElement).clone(true);
jQuery(oldElement).attr('id', fileId);
jQuery(oldElement).before(newElement);
jQuery(oldElement).appendTo(form);
就是在 clone() 括号中加个 true