我正在使用 html5 slice 和 webworker,但是当它进入 uploadFile 函数时,什么也没发生。什么都没有上传
<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
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();
}
}