- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个适用于 iOS 的 phonegap 应用程序,可以从服务器下载图像和 pdf 文件,因为它们必须在本地可用。
我使用 jQuery 进行解析以查找我需要下载的 Assets 列表,然后使用 Phonegap API 启动 FileTransfer,如下所示:
// assets is an array that has all pdf and image urls to be downloaded.
assets.map(downloadFile);
function downloadFile(file_url){
console.log('Downloading file:'+file_url);
var fileTransfer = new FileTransfer();
fileTransfer.download(
file_url,
get_local_file_path(file_url),
download_success,
function(error) {
stopspin();
console.log('ERROR downloading: '+ error.source);
$('#notify-user').html('Downloading failed. <a href="#" onclick="checkLatestIssue()">Try again?</a>')
}
);
}
现在, Assets 每次最多可达 50 个。我可以看到所有文件传输请求都立即发送,有时,一些文件传输请求超时,因此下载不完整,无法通过本地 Assets 呈现。
有没有一种方法可以使所有这些问题的下载连续进行大约 5 次并行下载?
最佳答案
我在我的应用程序上所做的是创建一个我需要下载的文件的全局数组,然后启动代码从数组的前面获取第一个元素并下载它。完成后,它会检查数组中是否还有要处理的项目,如果有,它会重新开始该过程。如果没有,它将完成下载并继续执行应用程序接下来需要执行的任何操作。
为我们的文件创建一个空白数组来下载data_to_process = [];
将图像和视频添加到从 API 返回的数组形式的“result”中
// add images to array
$.each( result.images_to_download, function( index, value ) {
index = index + 1;
data_to_process.push(
{
"type": "images",
"filename": value,
"msg": "Downloading image "+index+" of "+result.images_to_download.length+"..."
}
);
});
// add videos to array
$.each( result.videos_to_download, function( index, value ) {
index = index + 1;
data_to_process.push(
{
"type": "videos",
"filename": value,
"msg": "Downloading video "+index+" of "+result.videos_to_download.length+"..."
}
);
});
这是处理文件的代码注意:这里有一些不需要的代码,我用来根据文件的内容将文件放在某些目录中,更新下载进度的百分比指示器等:
// values for percentages
start_data_length = data_to_process.length;
current_iteration = 0;
// set writing to false
writing = false;
// what we do after each iteration in the data array
var finish_processing = function() {
// increment iteration count
current_iteration++;
// set percent value
var percent = current_iteration / start_data_length * 100;
percent = Math.round( percent );
$('#update_percent').html( percent );
// remove from array after process
data_to_process.splice( 0, 1 );
// set writing back to false so we can start another iteration
writing = false;
}
// checks if we need to process the next part of the array or not
var check_to_process = function() {
// console.log('checking if we need to process');
// if we still have items in the array to process
if ( data_to_process.length > 0 ) {
// if we're currently not working on an item
if ( writing === false ) {
// process next item in array
process_download_data();
} else {
// not ready yet, wait .5 seconds and check again
// console.log('busy - waiting to process');
setTimeout( check_to_process, 500 );
}
} else {
// console.log('nothing left to process');
}
}
// processes the downloaded data
var process_download_data = function() {
// set writing to true
writing = true;
// current item to process is first item in array
var current_item = data_to_process[0];
if ( current_item.msg ) {
$('#update_item').html(current_item.msg);
} else {
$('#update_item').html('Processing update data...');
}
// console.log( 'processing this: '+ current_item.filename );
// if something didn't work right..
var broke = function(error) {
// console.log( 'error' );
// console.log( error );
// cycle to next item in array
finish_processing();
}
// get the directory of current item's "type"
var get_directory = function( fileSystem ) {
fileSystem.root.getDirectory( current_item.type, {create:true,exclusive:false}, get_file, broke );
}
// get the file
var get_file = function( dirEntry ){
// if texts
if ( current_item.type == 'texts' ) {
dirEntry.getFile( current_item.filename+".txt", {create: true, exclusive: false}, create_text_writer, broke);
// if images
} else if ( current_item.type == 'images' ) {
var localFileName = current_item.filename.substring(current_item.filename.lastIndexOf('/')+1);
dirEntry.getFile( localFileName, {create: true, exclusive: false}, create_file_writer, broke);
// if videos
} else if ( current_item.type == 'videos' ) {
var localFileName = current_item.filename.substring(current_item.filename.lastIndexOf('/')+1);
dirEntry.getFile( localFileName, {create: true, exclusive: false}, create_file_writer, broke);
// if audios
} else if ( current_item.type == 'resources' ) {
var localFileName = current_item.filename.substring(current_item.filename.lastIndexOf('/')+1);
dirEntry.getFile( localFileName, {create: true, exclusive: false}, create_file_writer, broke);
// if audios
} else if ( current_item.type == 'audios' ) {
var localFileName = current_item.filename.substring(current_item.filename.lastIndexOf('/')+1);
dirEntry.getFile( localFileName, {create: true, exclusive: false}, create_file_writer, broke);
}
}
// write to file using filetransfer
var create_file_writer = function( fileEntry ) {
if ( current_item.remove === true ) {
fileEntry.remove( function(entry) {
// what to do when image is deleted
finish_processing();
},broke);
} else {
var localPath = fileEntry.fullPath;
// console.log('Writing to: '+localPath+' from: '+current_item.filename);
var ft = new FileTransfer();
ft.download( current_item.filename, localPath, function(entry) {
// what to do when image is downloaded
finish_processing();
},broke);
}
}
// create writer for "texts" type
var create_text_writer = function( fileEntry ) {
fileEntry.createWriter( write_text_file, broke );
}
// write to file for "texts" type
var write_text_file = function( writer ) {
// write new contents
writer.write( current_item.data );
// add item where it needs to be in the document
if ( current_item.append === true ) {
$('#'+current_item.marker).nextAll().remove();
// appending
// console.log('Appending data to: #'+current_item.id);
$('#'+current_item.id).append( current_item.data );
} else {
// adding
// console.log('Adding data to: #'+current_item.id);
$('#'+current_item.id).html( current_item.data );
}
// console.log('finished writing: '+current_item.filename);
finish_processing();
}
// request persistent filesystem
window.requestFileSystem( LocalFileSystem.PERSISTENT, 0, get_directory, broke );
// check if we need to process the next item yet
check_to_process();
}
// process data from the update
process_download_data();
关于ios - 在 iOS Phonegap 应用程序上序列化文件传输,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12734529/
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 7 年前。 Improve th
所以我正在开发一个黑 jack 程序,但我有点卡住了。我会警告大家,我对编程真的很陌生,而且,我正在项目中期......所以有一些松散的结局和未使用的变量,以及一些不必要的逻辑(用于测试),但这就是我
我正在尝试创建一个可用作 OpenGL 测试工具的示例程序。到目前为止,我的那个似乎可以工作,但似乎忽略了通过统一变量 MVPMatrix 传递的 MVP 矩阵。当我添加代码以读回制服并检查它是否确实
感谢您帮助我,这是有关我的代码的部分。 printf("Thank you, now please enter the logic gate"); scanf("%s", &C); if (C ==
public static void ejemplosString(String palabra){ char[] letras = palabra.toCharArray();
所以,我有一个 php 应用程序,通过 cgi 和 nginx 运行。我有一个 .jar 程序,用于在条形码打印机(Zebra)上打印条形码,猜猜看是什么!。 我的 php 应用程序使用 exec()
我遇到的唯一问题是 getAll() 方法,它似乎在 PersonnelController 类中的位置立即运行。我也曾在其他很多地方尝试过,但都没有成功。 setAll() 方法看起来不错,我已经测
我是一名优秀的程序员,十分优秀!