【问题标题】:html5 file upload from different folders从不同文件夹上传html5文件
【发布时间】:2014-05-09 15:51:34
【问题描述】:

我正在尝试制作一个 html5 图片上传器,但 html5 存在多次上传问题。

这是我使用的脚本:

function makeFileList() {
        var input = document.getElementById("filez");
        var ul = document.getElementById("file_wrap_list");
        while (ul.hasChildNodes()) {
            ul.removeChild(ul.firstChild);
        }
        for (var i = 0; i < input.files.length; i++) {
            var j = i+1;
            var li = document.createElement("li");
            li.innerHTML = j+'. '+input.files[i].name;
            ul.appendChild(li);
        }
        if(!ul.hasChildNodes()) {
            var li = document.createElement("li");
            li.innerHTML = 'No Files Selected';
            ul.appendChild(li);
        }
    }

HTML 标记:

&lt;input type="file" name="photo[]" id="filez" multiple onchange="makeFileList();"/&gt;

然后在提交时我上传并在 mysql 表中添加图像。我考虑过更改 js 脚本,以便每次选择 iamges 时,它们都会创建隐藏输入并通常显示它们的名称,以便客户知道他们选择了什么。然后在提交表单后,图像将被上传,但我不知道它是否会在实践中起作用,我的想法似乎有点奇怪。我想知道是否有人对如何更改 js 脚本有任何建议,以便每次用户单击并选择图像时,脚本都会在数组中注册图像。或者欢迎任何其他想法。

【问题讨论】:

    标签: html file-upload upload


    【解决方案1】:

    您对数组的想法很好。然后问题是确保用户尚未选择文件,并允许他们删除之前选择的文件。困难的部分是可靠地比较文件。

    I tweaked your code and managed to get it working here.

    // An array to store file references in
    var files = [];
    
    // Look in files to see if it's already in there
    function alreadySelected( newFile ){
        var match = false;
    
        // We can't just use files.indexOf( newFile ), since JS can't test file equality
        files.forEach( function compareProperties( oldFile ){
            var key;
            var oldProp;
            var newProp;
    
            for ( key in newFile ){
                if( newFile.hasOwnProperty( key ) ){
                    oldProp = oldFile[ key ];
                    newProp = newFile[ key ];
    
                    if( newProp !== oldProp ){
                        // The root of the problem: the same date will be a different date object.
                        if( newProp instanceof Date ){
                            if( newProp.getTime() !== oldProp.getTime() ){
                                return;
                            }
                        }
                        else {
                            return;
                        }
                    }
                }
            }
    
            match = true;
        } );
    
        return match;
    }
    
    window.makeFileList = function makeFileList() {
        var input = document.getElementById("filez");
        var ol    = document.getElementById("file_wrap_list");
    
        Array.prototype.forEach.call( input.files, function processFile( file ){
            // If the user's already added this file to the list, move on to the next.
            if( alreadySelected( file ) ){
                return;
            }
    
            var li = document.createElement("li");
            // We'll also create an 'X' link for users to remove files from the list
            var a  = document.createElement("a");
    
            li.innerHTML = file.name;
            a.innerHTML  = "X";
    
            a.addEventListener( "click", function removeFile( clickEvent ){
                clickEvent.preventDefault();
    
                // Find the file in the array and remove it
                files.splice( files.indexOf( file ), 1  );
                ol.removeChild( li );
            }, false ) ;
    
            files.push( file );
    
            li.appendChild( a );
            ol.appendChild( li );
        } );
    
        // Reset the value. Normal file inputs won't trigger the change event if the value is the same,
        // but a user might add a file, delete it, then try to add it again.
        input.value = '';
    };
    

    【讨论】:

    • 您的脚本在客户端完美运行,现在我正试图使其在服务器端运行,因为由于某种原因,当我提交表单时,图像发布数据在服务器端变为空跨度>
    • 我尝试了 console.log(files) 并给出了数组。但我认为问题可能出在 window.makeFileList 因为它发送的数组不是列表 FileList
    • 我发现了问题,最后一行:input.value = '';总是发送文件的空数组数据。我删除了它,现在它可以工作了。但是必须有一个解决方法,只是删除它不是解决方案,有什么想法吗?
    • 您最初的问题是文件输入无法从多个文件夹中选择文件,这就是我们将文件添加到数组的原因。您不想发送文件输入的内容,因为它只能从一个文件夹中获取文件。相反,您希望发送 files 数组中的每个文件。你自己提出了数组的想法!
    • 我访问了您的链接,尽管它在 jsfiddle 上运行良好,但如果我删除了第 15 行的检查“hasOwnProperty”,它只对我有用(在我的代码中)。出于某种原因,hasOwnProperty check 总是被评估为 false,所以我一次不能添加多个文件。
    【解决方案2】:

    我相信这个问题与我最近尝试回答的另一个问题相似。我的解决方案的链接在这里:HTML multiple file upload from different folders

    使用 HTML 和 jQuery,我能够创建一个表单,您可以在其中动态添加多个“浏览器”按钮。我还提供了一个 PHP 解决方案来处理服务器端的数据!

    【讨论】:

      猜你喜欢
      • 2019-02-12
      • 2014-09-13
      • 2016-05-23
      • 2011-04-05
      • 2016-03-11
      • 1970-01-01
      • 1970-01-01
      • 2017-05-17
      相关资源
      最近更新 更多