【发布时间】:2015-04-14 16:57:01
【问题描述】:
我正在开发一个需要离线工作的 Meteor Cordova 应用程序。
我正在使用 ground:db 离线缓存我的数据,它工作正常,除了图像。 我有一个使用 collectionFS 的图像集合。由于这些图像需要在离线时可用,我开发了某种本地同步来观察图像集合,当添加或更改某些图像时,使用 cordova 文件系统和文件传输将图像下载到本地存储。我在客户端集合中跟踪下载的图像。
在 a 模板中使用图像时,我会检查该图像是否在本地存在。如果是这样,我将本地文件路径传递给模板,否则我传递 url。
(android:http://meteor.local/:0) Not allowed to load local resource: file:///storage/emulated/0/brachot/AbsoluteBlackGepolijst.jpg
Meteor 移动应用访问本地文件系统是否存在某种问题?
这是我的一些相关代码:
Images.find().observe({
added: function(doc){
console.log('added: ' + doc.original.name);
var localImage = LocalImages.findOne(doc._id);
if (!localImage && window.fileSystem && window.fileSystem.root){
// create filepath for new file
var dir = window.fileSystem.root.getDirectory("brachot", {create: true, exclusive: false}, function(dirEntry){
var file = dirEntry.getFile(doc.original.name, {create: true, exclusive: false}, function(fileEntry){
var filePath = fileEntry.toURL();
// download the file to the filepath
var fileTransfer = new FileTransfer();
console.log('starting file download: ' + doc.url() + ' to ' + filePath);
fileTransfer.download(
doc.url(),
filePath,
function(){
// download image and save locally
LocalImages.insert({
_id: doc._id,
name: doc.original.name,
url: filePath
});
console.log('save');
},
function(error){
console.log('failed to save image: ' + filePath + ' (error: ' + error.http_status + ')');
}
);
});
}, function(error){
console.log(JSON.stringify(error));
});
Template.materials.helpers({
imageUrl: function(){
var image = LocalImages.findOne({name: this.image});
if (!image) {
image = Images.findOne({'original.name': this.image});
return image.url();
}
else {
return image.url;
}
}
});
【问题讨论】:
-
检查这个Github issue,简而言之,你需要运行
meteor --release cordova-41.0,因为其他两个人不工作,但试试看 -
我正在编写一个类似的应用程序。访问本地文件系统有进展吗?
标签: javascript cordova mobile meteor html5-filesystem