我显示div中的所有文件,这些文件是一个数组upfiles.使用
jquery中的每个显示带删除按钮的所有文件,当我单击删除按钮时,应从数组中删除相应的文件详细信息.
这是每个循环的jquery代码,用于从数组中删除文件详细信息
var int_loop = 1;
var display_removebutton="";
$(upfiles).each(function(index,file)
{
if(total_size > 1000) // size limit comparision
display_removebutton = "<img width='20px' style='cursor:pointer;' height='20px' id='remove_"+int_loop+"' src='images/DeleteRed.png' />"
size = Math.round( file.size / 1024 );
if(size > 1000)
{
if(size > 1024)
size_display = Math.round(size / 1024 * 100)/100 + ' mb';
else
size_display = size + ' kb';
alert(file.name+"("+size+")"+" will be removed atomatically from list. As it exceeds limit.");
}
if(size > 1024)
size_display = Math.round(size / 1024 * 100)/100 + ' mb'; // converted to mb
else
size_display = size + ' kb';
$('#total').append("<div id='div_selec"+int_loop+"'><b>File Name :</b> "+file.name + "<b> Size:</b>" + size_display + display_removebutton + "</div>" );
$("#remove_"+int_loop).click(function() {
var curr_id = this.id;
var id = curr_id.substr(7);
alert(id+'file name '+file.name);
$("#div_selec"+id).empty();
upfiles.splice(index,1) //updated as the suggested in comment
// delete upfiles[id];
alert(upfiles.length);
});
int_loop++;
});
编辑1:
其实我在jquery PHP中实现拖放文件上传.在验证中,如果所有文件的总大小大于1000kb,则显示删除按钮,哪个用户必须删除一些文件
编辑2:
我在控制台日志中收到此错误:TypeError:upfiles.splice不是函数
编辑3:
upfiles来自这个jquery drop事件:
$( '#total' ).bind( 'drop',function(event) {
event.stopPropagation();
event.preventDefault();
if( upfiles == 0 )
{
upfiles = event.originalEvent.dataTransfer.files;
console.log(upfiles); // in this console log it is displaying `FileList [File,File,File]` File is nothing but the files which i have dropped in a div
}
else {
if(confirm( "Drop: Do you want to clear files selected already?" ) == true) {
upfiles = event.originalEvent.dataTransfer.files;
$( '#filetoUpload' ).val('');
}
else
return;
}
$( "#filetoUpload" ).trigger( 'change' );
});
解决方法
未定义splice,因为upfiles的类型为FileList,而FileList的原型未定义它.但是,您可以使用var array = Array.prototype.slice.call(arrayLikeObject,0)将任何类似数组的对象转换为数组.现在数组有splice和所有其他数组方法.有关简短示例,请参见
http://jsfiddle.net/8e2s7/1/.