简体   繁体   English

Javascript Filereader-文件类型检查

[英]Javascript Filereader - File Type Checking

I'm currently trying to implement a new HTML5 driven upload site. 我目前正在尝试实施一个新的HTML5驱动的上传网站。 Out of security reasons I was usually quite restrictive regarding file types to be uploaded, so usually only ZIP files were accept from the back end. 出于安全原因,我通常对要上传的文件类型有严格的限制,因此通常仅后端接受ZIP文件。 As there seem to be many people around that seem to have difficulties using ZIP, I thought I'd give it a try and do something as follows: 由于周围似乎有很多人似乎在使用ZIP方面遇到困难,因此我想尝试一下,然后执行以下操作:

  • Select files to be uploaded 选择要上传的文件
  • Read 4 first bytes of each file to determine if it's a ZIP file (it's not fool proof, but never mind that). 读取每个文件的前4个字节,以确定它是否是ZIP文件(虽然不是万无一失,但不要紧)。
  • Upload ZIP files directly 直接上传ZIP文件
  • Take all the other files and create a ZIP archive on the fly and upload that newly created archive. 提取所有其他文件并即时创建一个ZIP存档,然后上传该新创建的存档。

The whole thing works well, except for the file type checking: 除了文件类型检查之外,整个过程都运行良好:

//'files' is an array of files retrieved from <input type='file'...for instance
    function checkFileType(files, callback) {
        for (var i = 0; i < files.length; i++) {

            var fileReader = new FileReader();

            fileReader.onloadend = function (e) {
                var arr = (new Uint8Array(e.target.result)).subarray(0, 4);
                var header = "";
                for (var j = 0; j < arr.length; j++) {
                    header += arr[j].toString(16);
                }

                if (header == '504b34')
                    ;
                else
                    callback("something");

            };


            var blob = files[i].slice(0, 4);
            fileReader.readAsArrayBuffer(blob);
        }
    }

I'm using "slice" here to only read the 4 first bytes (in case the file is huge, I don't want to mess up memory). 我在这里使用“切片”仅读取前四个字节(如果文件很大,我不想弄乱内存)。 The only thing that troubles me: Because of the slicing I lose the reference to the actual file in the array. 唯一困扰我的事情是:由于切片,我丢失了对数组中实际文件的引用。 I need to remove files that are being detected as ZIP from the array and start uploading them on the fly. 我需要从阵列中删除被检测为ZIP的文件,并开始动态上传它们。

Afterwards, all remaining files in the array shall be zipped. 之后,应压缩阵列中所有剩余的文件。 My approach doesn't seem to work because of that missing 'reference'... 由于缺少“参考”,我的方法似乎不起作用...

I guess I just found a simple solution, using a closure,... 我想我只是找到了一个简单的解决方案,使用闭包,...

function checkFileType(files, callback)
{
    for(var i = 0; i < files.length; i++) {
        var blob = files[i].slice(0, 4);
        var fileReader = new FileReader();

        fileReader.onload = (function(file_array, i)
        {
            return function(e) {
                var arr = (new Uint8Array(e.target.result)).subarray(0, 4);
                var header = "";

                for (var j = 0; j < arr.length; j++) {
                    header += arr[j].toString(16);
                }

                if (header == '504b34')
                    callback("ZIP");
                else
                    callback(e.name);
            };
        })(files, i);

        fileReader.readAsArrayBuffer(blob);
        console.log(fileReader.result);
    }
}

Like this, both the reference to the array and the index are available inside the 'onload' callback. 这样,对数组的引用和索引都可以在“ onload”回调中使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM