Javascript 添加时从队列中删除文件
我用的是blueimp的文件上传器。我想让用户只能上传一个文件(我不能使用Javascript 添加时从队列中删除文件,javascript,jquery,file-upload,jquery-file-upload,blueimp,Javascript,Jquery,File Upload,Jquery File Upload,Blueimp,我用的是blueimp的文件上传器。我想让用户只能上传一个文件(我不能使用maxNumberOfFiles,因为我没有使用UI插件) 如何使用户在选择要上载的新图像时,删除以前在队列中的文件,然后将新图像文件添加到队列中进行上载 以下是我到目前为止的情况: $('#fileupload').fileupload({ dataType: 'json', add: function(e, data) { var uploadErrors = []; v
maxNumberOfFiles
,因为我没有使用UI插件)
如何使用户在选择要上载的新图像时,删除以前在队列中的文件,然后将新图像文件添加到队列中进行上载
以下是我到目前为止的情况:
$('#fileupload').fileupload({
dataType: 'json',
add: function(e, data) {
var uploadErrors = [];
var acceptFileTypes = /^image\/(gif|jpe?g|png)$/i;
if (data.originalFiles[0]['type'].length && !acceptFileTypes.test(data.originalFiles[0]['type'])) {
uploadErrors.push('Invalid file type.');
} else if (data.originalFiles[0]['size'] > 1000000) {
uploadErrors.push('Image over size.');
}
if (uploadErrors.length > 0) {
$('.errors').html(uploadErrors);
} else {
data.submit();
}
},
progressall: function (e, data) {
var progress = parseInt(data.loaded / data.total * 100, 10);
$('.progress .bar').css('width', progress + '%');
}
});
您可以在
添加
操作之后操作文件数组数据.files
/数据.originalFiles
,按索引取消设置文件,如:
data.files = $.grep( data.files, function( element, index ) {
return index == 0;
// or
// return $.inArray(i, error_uploads_indexes) ==-1; // where error_uploads_indexes is array with files indexes You want to remove
});
// ...
data.submit();
如果可以,您可以设置
singleFileUploads:false
。您有按钮触发上传吗?