我正在使用js和html的Windows 8.1应用程序,该应用程序包含我需要多次安装和更新的大量大文件。所以我开发了一个fct来从usb设备更新到本地数据应用程序文件夹。它的工作正常,但是我的fct并不是以承诺为基础的,并且要跟进进度和完成并不容易...... 所以我正在研究一个全新的fct,但它不起作用; o(有人可以帮助我建立承诺结构吗?
我的实际代码:
app.copyTree = function (srcRootSorageFolder, destRootStorageFolder) {
console.log('copy tree Start...');
var fileToCopy = 0;
var fileCopied = 0;
var foldToCreate = 0;
var foldCreated = 0;
//-- init and wait promiseS ....
scanFolder(srcRootSorageFolder, destRootStorageFolder).then(
function () {
console.log('successfull copy !!');
console.log(fileCopied + ' fichier(s) copié(s) sur ' + fileToCopy);
console.log(foldCreated + ' dossier(s) créé(s) sur ' + foldToCreate);
},
function error(error) {
console.log('error copy' + error);
console.log(fileCopied + ' fichier(s) copié(s) sur ' + fileToCopy);
console.log(foldCreated + ' dossier(s) créé(s) sur ' + foldToCreate);
}
);
//--sub fct with promise to scan a folder and launch copy
function scanFolder(srcFoldStorage, destFoldStorage) {
console.log('scanFolder Start...');
var promises = [];
return new WinJS.Promise(function (complete, error) {
promises.push(
srcFoldStorage.getFilesAsync().then(function (filesList) {
fileToCopy += filesList.size;
copyFiles(filesList, destFoldStorage);
})
);
promises.push(
srcFoldStorage.getFoldersAsync().then(function (foldersList) {
foldToCreate += foldersList.size;
loopSubFolder(foldersList, destFoldStorage);
})
);
WinJS.Promise.join(promises).then(
function () {
complete();
},
error
);
});
}
//--sub fct with promise to copy all sub-folders in a folder to a destination
function loopSubFolder(foldersList, destStorFolder) {
console.log('loopSubFolder Start...');
var promises = [];
var collideOpt = Windows.Storage.CreationCollisionOption.openIfExists;
return new WinJS.Promise(function (complete, error) {
foldersList.forEach(function (reg) {
var foldName = reg.name;
promises.push(
destStorFolder.createFolderAsync(foldName, collideOpt).then(
function (newFoldStorage) {
foldCreated += 1;
scanFolder(reg, newFoldStorage);
})
);
});
WinJS.Promise.join(promises).then(
function () {
complete();
},
error
);
});
};
//--sub fct with promise to copy all file in a folder to a destination
function copyFiles(filesList, destStorFolder) {
console.log('copyFiles Start...');
var promises = [];
var collideOpt = Windows.Storage.CreationCollisionOption.replaceExisting;
return new WinJS.Promise(function (complete, error) {
filesList.forEach(function (reg) {
var fName = reg.name;
promises.push(
reg.copyAsync(destStorFolder, fName, collideOpt).then(fileCopied += 1)
);
});
WinJS.Promise.join(promises).then(
function () {
complete();
},
error
);
});
};
//--
};
感谢您的帮助
先生
答案 0 :(得分:0)
所以,承诺和递归并不是真正的朋友......就像上帝和邪恶......我改变了工作方式,找到了一个完全有效且更简单的解决方案。
我决定在我的源文件夹中搜索文件,然后查看完成的文件数...我不知道为什么我一开始就没有使用这个解决方案...这样,我可以取得一些进展,并确保所有文件都已完成。
如果它可以帮助另一个有相同需求的人,我的代码如下:
app.copyFolder = function (srcRootSorageFolder, destRootStorageFolder) {
//srcRootSorageFolder & destRootStorageFolder need to be StorageFolder ( IAsyncOperation<StorageFolder>)
var totalFiles = 0; //total files to copy
var totalSize = 0; //total octects to copy
var doneCopies = 0; // files copy terminated
var doneSize = 0; // octets copied
//Prepare query to Search all files (deep search / recursive ) in the srcRootSorageFolder to follow progress
var queryOptions = new Windows.Storage.Search.QueryOptions();
queryOptions.folderDepth = Windows.Storage.Search.FolderDepth.deep;
var query = srcRootSorageFolder.createFileQueryWithOptions(queryOptions);
//--sub function to prepare progress (counting files and size)
function prepareProgress(files) {
var promises = [];
return new WinJS.Promise(function (complete, error) {
files.forEach(function (file) {
promises.push(
file.getBasicPropertiesAsync().then(function (props) {
totalFiles += 1;
totalSize += props.size;
})
)
});
WinJS.Promise.join(promises).then(
function () {
complete(files);
},
error
);
});
}
//--sub function to copy files
function copyFiles(files) {
var promises = [];
var folderCollideOpt = Windows.Storage.CreationCollisionOption.openIfExists;
var fileCollideOpt = Windows.Storage.CreationCollisionOption.replaceExisting;
return new WinJS.Promise(function (complete, error) {
files.forEach(function (file) {
var destPath = file.path.split(srcRootSorageFolder.path); // get the folder tree to create directory tree of the source folder in the dest folder
destPath = destPath[destPath.length - 1]; //keeping last element of the array
destPath = destPath.substring(1, destPath.lastIndexOf('\\')); //removing file name en 1st slash(\)
var fName = file.name;
promises.push(
destRootStorageFolder.createFolderAsync(destPath, folderCollideOpt).then(
function (destStorage) {
//dest folder ready, initialising ... start copying file
file.copyAsync(destStorage, fName, fileCollideOpt).then(
function (newFile) {
updateProgress(file,newFile);
});
}
)
)
});
WinJS.Promise.join(promises).then(
function () {
complete(files);
},
error
);
});
}
//--sub function to follow progress and defined if all copy are completed
function updateProgress(file,newFile) {
return new WinJS.Promise(function (complete, error) {
newfiles.getBasicPropertiesAsync().then(function (newProps) { console.log('ok (copy):' + newfiles.name + ':' + newProps.size); });
file.getBasicPropertiesAsync().then(function (props) {
doneCopies += 1;
doneSize += props.size;
console.log('ok (source):' + file.name + ':' + props.size);
//progress
var copiesProgress = Math.round((doneSize / totalSize) * 100 * 100) / 100; // copy percent with 2 decimals
console.log('progress: ' + copiesProgress + '%');
//completed action
if (doneCopies == totalFiles) {
console.log('Copy Done');
}
});
});
}
//--initialising process
query.getFilesAsync().then(prepareProgress).then(copyFiles).then(console.log('Copy Start....'));
};
我希望你喜欢,如果你有意见让它变得更好,我会喜欢! 感谢
先生