调用asycn下载文件
asycn(
arr,
function(item, next) {createDownload(item, next);},
function() {console.log('end');}
);
function asycn(list, cb_exec, cb_end) {
var each = function(_list, cb) {
if (_list.length < 1) {
asycnflag = false;
//下载动画结束
$.hideIndicator();
cb_end();
return $.confirm('是否打开下载的文件', '下载文件', function() {
// $.popup('.popup-about');
$.router.loadPage('download.html');
$(document).on('pageInit', '#downloadpage', function() {
var downloads = document.getElementById('_downloads');
updateRootItem(downloads);
});
});
}
cb(_list.shift(), function() {
each(list, cb);
});
}
each(list, cb_exec);
};
//创建下载
function createDownload(item, next) {
console.log(item)
var dtask = plus.downloader.createDownload(url+ item, {}, function(d, status) {
// 下载完成
if (status == 200) {
next();
} else {
$.alert("下载失败", function() {
next();
})
}
});
dtask.start();
}
//打开文件基本没有变和你们新建的项目上的代码是一样的
var dir = "",
root = [],
current = null,
parent = null,
pitem = null,
list = null;
//文件列表
function updateList(entries) {
console.log(entries)
entries.sort(sortCompareEntry)
var filelt = $('<div>');
for (i = 0; i < entries.length; i++) {
var card = $('<div>', {
'class': 'card'
});
var cardct = $('<div>', {
'class': 'card-content'
});
var lsbk = $('<div>', {
'class': 'list-block'
});
var ul = $('<ul>');
var li = $('<li>');
card.entry = entries[i];
var itemct = $('<a>', {
'class': 'item-content item-link',
'id': card.entry.name + ''
});
itemct.click(function() {
updateRootItem(this);
});
var itemma = $('<div>', {
'class': 'item-media'
});
itemma.append('<img src=../../img/views/flows/ffile.png></i>');
var itemir = $('<div>', {
'class': 'item-inner'
});
var itemte = $('<div>', {
'class': 'item-title'
});
itemte.text(card.entry.name);
itemir.append(itemte);
itemct.append(itemma).append(itemir);
li.append(itemct);
ul.append(li);
lsbk.append(ul);
cardct.append(lsbk);
card.append(cardct);
filelt.append(card);
}
$('#_downloads').html(filelt);
}
//打开文件
function openDir(item) {
var entry = item.entry;
console.log(item)
if (!entry) {
console.log("Open directory \"" + item.id + "\" with null!");
return;
}
if (entry.isDirectory) {
console.log("Open directory: \"" + dir + item.id + "\"");
var dirReader = entry.createReader();
dirReader.readEntries(function(entries) {
parent = current;
current = item.entry;
dir = entry.toURL() + "/";
// Dispaly up to parent item
//pitem.style.display = "block";
// Update ui
updateList(entries);
}, function(e) {
console.log("Read directory " + item.id + " failed: " + e.message);
});
} else {
console.log("Open file: \"" + dir + item.id + "\"");
plus.runtime.openFile(dir + item.id, {}, function(e) {
plus.nativeUI.alert("无法打开此文件:" + e.emssage);
});
}
}
//排序规则
function sortCompareEntry(a, b) {
if (a.isDirectory && b.isFile) {
return -1;
} else if (a.isFile && b.isDirectory) {
return 1;
} else {
return a.name - b.name;
}
}
//绑定entry
function updateRootItem(item) {
plus.io.resolveLocalFileSystemURL(dir + item.id, function(entry) {
root.push(entry);
item.entry = entry;
updateInf(item, entry);
openDir(item);
}, function(e) {
});
}
function updateInf(item, entry) {
entry.getMetadata(function(metadata) {
var inf = item.querySelector(".finf");
if (entry.isDirectory) {
} else {
}
}, function(e) {
outLine("Get metadata " + entry.name + " failed: " + e.message);
}, false);
}
7***@qq.com (作者)
我用hbuilder新建的移动项目 file页面的文件系统打开_dowloads下面的文件也是打不开的报错“无法打开此文件 undefined”,试了一下只有www目录下的文件能打开
2016-09-02 11:16
Android_磊子
回复 7***@qq.com:同样格式的文件吗?如何让我们能重现问题。
2016-09-07 11:12