所以这是我的代码,我正在努力上传分块文件,我有一个2D数组,数组中的每个单元格都包含另一个数组,其中包含每个文件信息,然后是实际的文件数据,我无法在输入完成函数后立即发出http请求,而不是在执行for循环后调用它。我无法找到使用IIFE和闭包的工作。请指导。这是代码:
function chunkBinary(chunkFile){
var reader = new FileReader();
var deferred = $.Deferred();
reader.onloadend = (function(reader)
{
return function()
{
deferred.resolve(reader.result);
}
})(reader);
reader.readAsBinaryString(chunkFile);
console.log('returning chunk data as binary');
return deferred.promise();
}
//changing screen and going to next step
function showUploadProgress(){
//display the new body first with all files
for (var i=0; i<chunk.length; i++) {
console.log('I is: ' + i);
console.log(chunk[i]);
globalfileSize = chunk[i][2];
//var fileType = chunk[i][2];
globalfileName = chunk[i][3];
globalChunkLen = chunk[i].length;
var pctLoaded = 0;
//call upload service on chunks (chunk[i][0..3] is file info)
for(j=4; j < chunk[i].length;j++){
console.log('entered for-loop 2 which loops over each chunk');
$.when(chunkBinary(chunk[i][j]))
.done(function(result){
chunkInBinary = result;
var md = configOptions.data.buildPrice.dataSource['uploadEquipmentPhotosDocuments'].$d($_QUOTES.obj_quote_info.quoteNumber, $_scope.equipInfo.summary.id);
var chunkType = '';
if( j==4){ chunkType = 'First'; }
else if( j == globalChunkLen-1) { chunkType = 'Last'; }
else { chunkType = 'Middle'; }
var data = {"file":chunkInBinary,"chunkType":chunkType,"fileName":globalfileName,"fileSize":globalfileSize};
console.log('making http request for chunk');
return $_http(
{url: md,
method: 'POST',
data:JSON.stringify(data),
headers: {'Content-Type': 'multipart/form-data'}
}).success(function (data, status, headers, config) {
console.log(j,i);
console.log('chunk sent');
console.log(chunkType);
console.log(globalfileName);
console.log(globalfileSize);
pctLoaded = (j+1)*100/(chunk[i].length -4);
console.log(pctLoaded);
}).error(function (data, status, headers, config) {
console.log(j,i);
console.log('chunk error');
console.log(chunkType);
console.log(globalfileName);
console.log(globalfileSize);
});
});
}
console.log('I incremented');
}
}
对于它的价值,这是我尝试上传一个文件后控制台的屏幕截图。