如何从多阵列文件的详细信息 [英] how to remove file details from multi array

查看:121
本文介绍了如何从多阵列文件的详细信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个div这是在一个阵列来林显示所有文件 upfiles 。每个使用jQuery中显示所有删除按钮的文件,当我的删除按钮各自的文件细节应该从一个数组中删除点击。

下面是jQuery的code分别环路即时通讯特林删除阵列文件的详细信息。

  VAR int_loop = 1;
        变种display_removebutton =;
    $(upfiles)。每个(函数(索引,文件)
        {
            如果(TOTAL_SIZE> 1000)//大小限制比较
                 display_removebutton =< IMG宽度='20px的风格=光标:指针;'高度='20px的'ID ='删除_+ int_loop +'SRC =图像/ DeleteRed.png/>中
            大小= Math.round(file.size / 1024);
            如果(大小> 1000)
            {
                如果(大小> 1024)
                    size_display = Math.round(规格/ 1024 * 100)/ 100 +'MB';
                其他
                    size_display =大小+'KB';
                警报(file.name +(+规模+)+将被atomatically从名单超过限制去掉。);
            }
            如果(大小> 1024)
                    size_display = Math.round(规格/ 1024 * 100)/ 100 +'MB'; //转换为MB
            其他
                    size_display =大小+'KB';
            $('#共')追加(< D​​IV ID ='div_selec+ int_loop +'>< B>文件名:< / B>中。+ file.name +< B>尺寸: < / b>中+ size_display + display_removebutton +< / DIV>中);
            $(#删除_+ int_loop)。点击(函数(){                VAR curr_id = this.id;
                变种的id = curr_id.substr(7);
                警报(ID +'文件名'+ file.name);
               $(#div_selec+ id)的.empty();
                   upfiles.splice(指数1)//更新为注释建议
//删除upfiles [ID]
                警报(upfiles.length);            });
             int_loop ++;
    });

编辑1:

其实即时通讯执行拖放jQuery的PHP下降文件上传。在确认是否所有文件的总大小超过1000KB IM更大的显示删除按钮,用户必须删除一些文件

编辑2:

即时得到这个错误在控制台日志:类型错误:upfiles.splice不是一个函数

编辑3:

upfiles从这个jQuery drop事件到来:

  $('#合计').bind('降',函数(事件){        event.stopPropagation();
        。事件preventDefault();        如果(upfiles == 0)
                {
                upfiles = event.originalEvent.dataTransfer.files;                                的console.log(upfiles); //在这个控制台登录它会显示'文件清单[文件,文件,文件,文件,文件,文件,文件]`文件是什么,但我在一个div已经下降了文件
                }
        其他{
            如果(确认(降:你要选择的文件已经明确?)==真){
                upfiles = event.originalEvent.dataTransfer.files;
                $('#fileToUpload').VAL('');
            }
            其他
                返回;
        }
        $(#fileToUpload).trigger('变');
});


解决方案

拼接没有定义,因为 upfiles 是键入文件清单文件清单的原型没有定义它。然而,你可以的转换的使用任何类似数组对象数组 VAR阵列= Array.prototype.slice.call(arrayLikeObject,0)。现在阵列拼接和所有其他阵列方法。请参见 http://jsfiddle.net/8e2s7/1/ 一个简单的例子。

Im display all the files in a div which is coming in an array upfiles . Using each in jquery displaying all the files with delete button, when i click on the delete button that respective file details should be deleted from an array.

Here is the jquery code each loop which im tring to delete file details from array

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++;
    });

Edited 1:

Actually im implementing drag and drop file upload in jquery php. In validation if total size of all files is greater than 1000kb im displaying delete button, which user has to delete some of the files

Edited 2 :

Im getting this error in console log : TypeError: upfiles.splice is not a function

Edited 3 :

upfiles is coming from this jquery drop event:

$( '#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, 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 is not defined because upfiles is of type FileList and the prototype of FileList doesn't define it. However you can convert any array-like object to an array using var array = Array.prototype.slice.call(arrayLikeObject, 0). Now array has splice and all other array methods. See http://jsfiddle.net/8e2s7/1/ for a short example.

这篇关于如何从多阵列文件的详细信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆