0
我遇到了一個問題,當我將jpg保存爲Windows上的八位字節流時,隨後的讀取將返回一個大小爲0的文件。我可以看到該文件我的硬盤,文件看起來不錯,但讀取文件失敗。Cordova FileEntry.file()返回大小0
下面的代碼是我用來與文件系統進行交互的,有些黑客試圖解決這個問題。
function downloadFileFromUrl(url, localPath, contentType, successCallback, failureCallback) {
var xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.responseType = 'blob';
xhr.onload = function() {
if (this.status == 200) {
localPath = encodeURI(localPath);
writePersistantFile(localPath, this.response, true, contentType,
function (fileEntry) {
if (successCallback != undefined) {
successCallback(fileEntry);
}
},
function (error) {
if (failureCallback != undefined) {
failureCallback(error);
}
});
}
};
xhr.send();
}
function createDirectoryRecursively(path, parentDir, callback, errorCallback) {
if (path == null || path.length == 0) {
callback(parentDir);
return;
}
var index = path.indexOf('/');
var dirName = null;
var remainder = null;
if (index <= 0) {
dirName = path;
} else {
dirName = path.substring(0, index);
remainder = path.substring(index + 1);
}
parentDir.getDirectory(dirName, { create: true, exclusive: true }, function (dirEntry) {
self.createDirectoryRecursively(remainder, dirEntry, callback, errorCallback);
}, function (error) {
if (error.code == 12) {
parentDir.getDirectory(dirName, { create: false }, function (dirEntry) {
self.createDirectoryRecursively(remainder, dirEntry, callback, errorCallback);
}, function (error2) {
errorCallback(error);
});
return;
}
errorCallback(error);
});
}
function getFile(fileName, parentDir, callback, errorCallback) {
if (parentDir == null) {
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0,
function (fs) {
getFile(fileName, fs.root, callback, errorCallback);
},
function (error) {
});
return;
}
if (fileName.indexOf('/') >= 0) {
var dirName = fileName.substring(0, fileName.lastIndexOf('/'));
createDirectoryRecursively(dirName, parentDir,
function (directory) {
var name = fileName.substring(fileName.lastIndexOf('/') + 1);
getFile(name, directory, callback, errorCallback);
}, errorCallback);
return;
}
parentDir.getFile(fileName, { create: true },
function (fileEntry) {
console.log('obtained file');
callback(fileEntry);
},
function (error) {
if (error.code == 1) {
parentDir.getFile(fileName, { create: false },
function (fileEntry) {
console.log('obtained file');
callback(fileEntry);
},
function (error) {
if (error.code == 12) {
return;
}
errorCallback(error);
});
return;
}
errorCallback(error);
});
}
function readPersistantFile(filePath, successCallback, errorCallback) {
getFile(filePath, null,
function (fileEntry) {
console.log('obtained file');
fileEntry.file(function (file) {
console.log('file size: ' + file.size);
var reader = new FileReader();
reader.onloadend = function() {
console.log("Successful file read");
if (successCallback != undefined) {
successCallback(this.result);
}
};
var extension = filePath.substring(filePath.lastIndexOf('.') + 1);
if (extension == 'json') {
reader.readAsText(file);
} else {
reader.readAsDataURL(file);
}
});
},
function (error) {
console.log('An error occured while getting the file');
console.error(error);
if (errorCallback != undefined) {
errorCallback(error);
}
});
}
function writePersistantFile(filePath, data, isBinary, contentType, successCallback, errorCallback) {
var dataSize = (data.length != undefined) ? data.length : (data.size != undefined)? data.size : 5 * 1024 * 1024;
window.requestFileSystem(LocalFileSystem.PERSISTENT, dataSize, function (fs) {
console.log('file system opened: ' + fs.name);
var lastIndex = filePath.lastIndexOf('/');
var directory = filePath.substring(0, lastIndex);
createDirectoryRecursively(directory, fs.root, function (parentDir) {
console.log('Directory created');
var fileName = filePath.substring(lastIndex + 1);
getFile(fileName, parentDir,
function (fileEntry) {
console.log('obtained file');
fileEntry.createWriter(function (fileWriter) {
console.log('created writer');
if (typeof (data) == 'string') {
fileWriter.write(new Blob([data], { type: contentType }));
} else {
fileWriter.write(data);
}
if (successCallback != undefined) {
successCallback(fileEntry);
}
});
},
function (error) {
console.log('An error occured while getting the file');
console.error(error);
if (errorCallback != undefined) {
errorCallback(error);
}
});
}, function (error) {
console.log('An error occured while saving the file');
console.error(error);
if (errorCallback != undefined) {
errorCallback(error);
}
});
});
}