本文介绍了html5块和webworker不上传任何内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用html5 slice和webworker,但是当它使用uploadFile函数时,什么也没发生.什么都没有上传

I was working with html5 slice and webworker but when it goes to uploadFile function, nothing happened. nothing being upload

<html>
    <head>
        <title>Upload Files using XMLHttpRequest</title>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>     
    </head>

    <body>
        <form id="fileuploader" enctype="multipart/form-data" method="post" action="upload.php">
            <label for="fileToUpload">Select Files to Upload</label><br />

            <input type="file" name="fileToUpload[]" multiple="" id="fileToUpload" onchange="fileList();"/><br />

            <input type="button" onclick="sendRequest();" value="Upload" />

            <!-- a place for File Listing -->
            <div id="fileList">              
            </div>           
        </form>
    </body>
</html>

<script type="text/javascript">

    function sendRequest() {
    var worker = new Worker("fileupload.js");

    worker.onmessage = function(e) {
        alert(e.data);
    }

    var file = document.getElementById('fileToUpload');

    for(var i = 0; i < file.files.length; i++) { 
        worker.postMessage(file.files[i]);  
    }
}

以及fileupload.js

and for the fileupload.js

var file;
var p = true;

function uploadFile(blobFile, fileName, filePart, totalChunks) {         
    var xhr = new XMLHttpRequest();                
    xhr.open("POST", "upload.php"+"?"+"file="+fileName + filePart, true);

    xhr.onload = function(e) {
    };


    xhr.send(blobFile);
}   

function process() {
    var blob = file;
    var originalFileName = blob.name;
    var filePart = 0

    const BYTES_PER_CHUNK = 100 * 1024 * 1024; // 100MB chunk sizes.
    var realFileSize = blob.size;

    var start = 0;
    var end = BYTES_PER_CHUNK;

    totalChunks = Math.ceil(realFileSize / BYTES_PER_CHUNK);

    while( start < realFileSize ) {
        //self.postMessage(start);
        //self.postMessage("test");

        var chunk = blob.slice(start, end);

        uploadFile(chunk, originalFileName, filePart, totalChunks);

        filePart++;
        start = end;
        end = start + BYTES_PER_CHUNK;
    }
}


self.onmessage = function(e) {
    file = e.data;
    /*for (var j = 0; j < e.data.length; j++) {
        files.push(e.data[j]);
    }*/

    if (p) {
        process();
    }
}

推荐答案

总结代码:

 if (!blob.webkitSlice && !blob.mozSlice) {
     var chunk = blob.mozSlice(start, end);
     // or: var chunk = blob.webkitSlice(start, end);
 }

这是您从中获得该错误的行,并且很明显,您需要在那里获取异常.

This is the line where you get that error from, and it should be obvious that you need to get an exception there.

当然,对于那些浏览器的旧版本,您可能应该检查前缀功能.但是,如果功能"检测不匹配,则将需要使用普通功能-可能是因为这些供应商已删除了前缀,或者代码是在某些其他浏览器(Opera,IE)中执行的.另请参见 浏览器兼容性:有关切片的说明( )实施在MDN .

Sure, for older versions of those browsers you probably should check for the prefixed functions. But if the "feature" detection has not matched, you will need to use the plain function - may it be because those vendors have dropped the prefixes, or the code is executed in some other browser (Opera, IE). See also Browser compatibility: Notes on the slice() implementations at MDN.

因此,使用此:

var slice = blob.webkitSlice || blob.mozSlice || blob.slice;
var chunk = slice.call(blob, start, end);

这篇关于html5块和webworker不上传任何内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 04:18