如何使用blueimp jquery上传插件获取每个块的md5迭代md5总和

29

我需要计算并发送一个迭代的md5哈希到我的上传API,但我不知道该怎么做。

我正在使用这里找到的教程:

http://tutorialzine.com/2013/05/mini-ajax-file-upload-form/

以及 blueimp jquery上传插件。

对于只发送一个文件(文件大小小于块大小),一切都正常工作。 但如果文件被分块,则我不知道如何捕获块以获取其md5。

最后,我必须按照这里描述的方式迭代地进行md5:

https://code.google.com/p/crypto-js/#Progressive_Hashing

$('#upload').fileupload({

    // This element will accept file drag/drop uploading
    dropZone: $('#drop'),

    type        : GLOBAL_FORM_METHOD,
    method      : "post",   // Type of data-send-method
    dataType    : "json",   // Type of data to recieve from api-call
    maxChunkSize: GLOBAL_CHUNK_SIZE,
    multipart   : true,

    // This function is called when a file is added to the queue;
    // either via the browse button, or via drag/drop:
    add: function (e, data)
    {
        var reader = new FileReader();
        var file = data.files[0];
        var jqXHR;

        var tpl = $('<li class="working"><input type="text" value="0" data-width="48" data-height="48"'+
            ' data-fgColor="#0788a5" data-readOnly="1" data-bgColor="#3e4043" /><p></p><div class="msg"></div><span></span></li>');

        // Append the file name and file size
        tpl.find('p')
            .text( file.name )
            .append('<i>' + formatFileSize( file.size ) + '</i>');

        // Add the HTML to the UL element
        data.context = tpl.appendTo(ul);

        // Initialize the knob plugin
        tpl.find('input').knob();

        // Listen for clicks on the cancel icon
        tpl.find('span').click(function()
        {
            if( tpl.hasClass('working') )
            {
                jqXHR.abort();
            }

            tpl.fadeOut( function()
            {
                tpl.remove();
            });
        });

        // Prevent XHR from sending data in "multipart/formData"
        data.postMessage = data.files[0].type;
        data.contentType = data.files[0].type;

        var chunksize = GLOBAL_CHUNK_SIZE > file.size ? file.size : GLOBAL_CHUNK_SIZE;

        // Describe the FileReader-DataLoad-Event
        reader.onload = function( event ) 
        {
            var binary = event.target.result;
            var md5 = CryptoJS.MD5(binary).toString();

            data.url += "&md5sum=" + md5;

            // D A T A   S E N D 
            jqXHR = data.submit();
        };

        // ADD url to XHR-object
        data.url = GLOBAL_FORM_ACTION;
        data.url += "?etf_id=" + GLOBAL_FOLDER_ID;
        data.url += "&file_title=" + file.name;

        // If the file will be send in one piece...
        if( GLOBAL_CHUNK_SIZE > file.size )
        {
            // ADD url-parameter to XHR-object
            data.url += "&size_chunk_start=" + 0;
            data.url += "&size_chunk_length=" + chunksize;
        }
        // This part for the chunks must be in "beforeSend"-Callback,
        // because, the chunk-related size-data is undefined in this case
        // but available there.

        // ADD url-parameter to XHR-object
        data.url += "&size_final=" + file.size;

        // Read md5-sum and send the file / chunk...
        // On multipart "file" is a chunk !
        reader.readAsBinaryString( file );
    },

    beforeSend : function(e, data)
    {
        var file = data.files[0];

        this.find(".msg").hide();

        // If the file will be send as chunks...
        if( GLOBAL_CHUNK_SIZE < file.size )
        {
            console.log( "Chunk data: ", data.uploadedBytes, data.chunkSize, file.size, data );

            // ADD url-parameter to XHR-object
            data.url += "&size_chunk_start=" + data.uploadedBytes;
            data.url += "&size_chunk_length=" + data.chunkSize;

            if( typeof this.attr('session_id') !== "undefined" )
                data.url += "&session_id=" + this.attr( 'session_id' );
        }

    });

我希望你能帮忙翻译,这样我就可以自己解决问题了。


寻找类似的功能。丹尼尔,你找到解决方案了吗? - Awesome
是的,我写了一个完全不同的上传脚本。 - Daniel
@Daniel 只是一个建议:你可以用你的新代码回答自己的问题,这样其他人也会受益 ;) - graciano
16
请关闭它。否则,它将永远留在“未回答的问题”列表中。 - gibatronic
怎么办?我找不到任何可能性。 - Daniel
显示剩余2条评论
1个回答

2
这是一个类似问题的答案,提供了一种实现方法。也有一些库可以为您完成此操作,例如CryptoJS。
要点在于您需要用0字节填充文件,以达到可被块长度整除的某个长度,然后逐块将数据读入缓冲区,并对该数据进行哈希处理。然后您需要为每个读取的块附加哈希值。
参考链接:MD5 Hash a large file incrementally

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接