Javascript DOM鼠标事件不适用于IE和Mozilla

时间:2012-04-04 11:22:01

标签: javascript dom file-upload

使用自定义文件上传应用程序。我有两个主要问题:

  1. 以下给出的代码不是为Mozilla和IE打开文件对话框。
  2. 在Chrome中它可以工作,但是当我选择首次点击时的文件时,它永远不会将文件添加到正文中。但是在第二次点击时,它会将首次点击浏览的文件添加到正文中。
  3. 对于上述问题的任何帮助将不胜感激。

    function perform1Click(node) {
    
                alert("INIT");
                var evt = document.createEvent("MouseEvents");
                evt.initEvent("click", true, false);
                node.dispatchEvent(evt);
    
                alert(3)
                getFile(evt);
    
            }
    
            function getFile(event) {
    
                var files = event.target.files;
                var totalSize = 0;
    
                if (totalSize > 1024*10) {
    
                    alert('Total size exceed 1 Mb.');
                    return;
                }
                //alert(files)
                //alert(files.length);
                for (var i = 0, f; f = files[i]; i++) {
    
                    displayFileList(f.name, f.size);
                    totalSize = totalSize+f.size;
                }
            }
    
            function displayFileList(name, size) {
    
                if (name != '') {
    
                    var top_plugin = document.getElementById('top_plugin');
    
                    // create tag 
                    var ptag = document.createElement("p");
    
                    // create div
                    var divBox = document.createElement("div");
                    divBox.setAttribute('class', 'divBox');
    
                    // create input[type='checkbox']
                    var inputCheckBox = document.createElement("input");
                    inputCheckBox.setAttribute('type', 'checkbox');
                    inputCheckBox.setAttribute('id', 'checkboxClass')
    
                    // add checkbox to div.
                    divBox.appendChild(inputCheckBox);
    
                    // create text node for divBox and add it to divBox.
                    var txtNode = document.createTextNode(name);
                    divBox.appendChild(txtNode)
    
                    var sizeDivBox = document.createElement("p");
                    sizeDivBox.setAttribute('style', 'clear:both; display: inline-block;');
    
                    var txtSizeNode = document.createTextNode(size);
                    sizeDivBox.appendChild(txtSizeNode);
                    divBox.appendChild(sizeDivBox);
    
                    // add divBox to ptag.
                    ptag.appendChild(divBox);
                    //ptag.appendChild(divTxt);
    
                    // add ptag to top_plugin div.
                    top_plugin.appendChild(ptag);
                }
    
                // if file value is not null, make it blank.
                if (name != '')
                {
                    name = '';
                }
            }
    

1 个答案:

答案 0 :(得分:0)

我得到了同样问题的解决方案。请在下面找到新代码。

   function uploadDFiles() {

        var file = document.getElementById('_file');
        file.click();

        try {
            file.addEventListener("change", getFileName);
        }
        catch (e) {
            file.attachEvent("onclick", getFileNameOnIE);
            alert("Error:: "+e.description);
        }
    }

    function getFileName(event) {

        var files = event.target.files;
        for (var i = 0, f; f = files[i]; i++) {

            var fileName = f.name;
            var fileSize = f.size;

            var fSize = bytesToSize(fileSize, 2);

            displayFileList(fileName, fSize);
        }
    }

但现在我遇到了新问题。此代码在IE中不起作用。对于IE,我使用的是attachEvent方法,但它不起作用。请在下面找到代码:

    function getFileNameOnIE(event) {

        alert(event.type);
        var files = event.target;
        alert(files.length);

        for (var i = 0, f; f = files[i]; i++) {

            displayFileList(f.name, f.size);
        }
    }

有人能为我提供同样的解决方案吗?

-

韩国社交协会 巴勒特