我正在iOS上开发应用程序。我为我的应用程序使用Phonegap和jQuery Mobile。目前,当用户单击图像或使用Phonegap从图库中选择图像并在本地保存URI时,我正在检索图像的URI。我需要通过在Base64字符串中转换它们来在服务器上提交这些图像。为了将它们转换成Base64,我使用Phonegap提供的示例。使用Phonegap将图像转换为iOS上的Base64字符串
<!DOCTYPE html>
<html>
<head>
<title>FileReader Example</title>
<script type="text/javascript" charset="utf-8" src="cordova-1.7.0.js"></script>
<script type="text/javascript" charset="utf-8">
// Wait for Cordova to load
//
function onLoad() {
document.addEventListener("deviceready", onDeviceReady, false);
}
// Cordova is ready
//
function onDeviceReady() {
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
}
function gotFS(fileSystem) {
fileSystem.root.getFile("readme.txt", null, gotFileEntry, fail);
}
function gotFileEntry(fileEntry) {
fileEntry.file(gotFile, fail);
}
function gotFile(file){
readDataUrl(file);
readAsText(file);
}
function readDataUrl(file) {
var reader = new FileReader();
reader.onloadend = function(evt) {
console.log("Read as data URL");
console.log(evt.target.result);
};
reader.readAsDataURL(file);
}
function readAsText(file) {
var reader = new FileReader();
reader.onloadend = function(evt) {
console.log("Read as text");
console.log(evt.target.result);
};
reader.readAsText(file);
}
function fail(evt) {
console.log(evt.target.error.code);
}
</script>
</head>
<body>
<h1>Example</h1>
<p>Read File</p>
</body>
</html>
在该示例中,代替“README.TXT”我在模拟器fileSystem.root.getFile("image.png", null, gotFileEntry, fail);
传递一个本地图像URI进行测试。不过,我得到以下错误:
Error in error callback : File2 = TypeError:'undefined' is not an object.
我也试过了图像的绝对路径,但得到了同样的错误。我不明白会出现什么问题?我错过了什么?我需要尽快解决这个问题。
任何帮助,高度赞赏。
谢谢。