我试图从手机上的相册中获取base64,但我无法让它正常工作:
我试过这个:
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSystem) {
console.log("0");
fileSystem.root.getFile(imageURI, null, function(fileEntry) {
console.log("1");
fileEntry.file(function(file) {
console.log("2");
var reader = new FileReader();
reader.onloadend = function(evt) {
console.log("Read complete!");
image64.value = Base64.encode(evt.target.result);
};
reader.readAsText(file);
}, failFile);
}, failFile);
}, failSystem);虽然图像显示正确..。我从这个函数收到一个错误:
fileSystem.root.getFile(imageURI, null, function(fileEntry)错误是:FileError.ENCODING_ERR
我知道密码看起来不好看。但我不知道如何从Base64中获取imageURI编码。
发布于 2012-03-06 13:29:04
我在谷歌集团中找到了解决方案。我对它做了一点修改,结果是:
var gotFileEntry = function(fileEntry) {
console.log("got image file entry: " + fileEntry.fullPath);
fileEntry.file( function(file) {
var reader = new FileReader();
reader.onloadend = function(evt) {
console.log("Read complete!");
image64.value = Base64.encode(evt.target.result);
};
reader.readAsText(file);
}, failFile);
};
window.resolveLocalFileSystemURI(imageURI, gotFileEntry, failSystem); 注意:读取普通的5 5MPixel图像大约需要20秒,而Base64则需要10-15才能对其进行编码。
发布于 2012-08-29 09:26:50
我必须更改SERPRO works...but中的上述方法
reader.readAsText(file);
to
reader.readAsDataURL(file);因此,行
image64.value = Base64.encode(evt.target.result);可以删除base64结果,并将其直接提取为
image64.value = evt.target.result;发布于 2018-02-13 16:50:09
cordova-plugin文件实现了HTML5文件API,并且使用了回调API。
function getContentAsBase64(fileUrl) {
//Using $q to change the API so that getContentAsBase64 returns a promise
var deferred = $q.defer();
//function to call when either resolve or retrieval fails
var fail = function (error) {
errorHandler(error);
deferred.reject(error);
}
//function to call when resolve file succeeded
//we have a FileEntry - get the file,
var fileResolved = function (fileEntry) {
fileEntry.file(fileSuccess, fail);
}
//function to call when file successfully retrieved
//convert to base64 string
var fileSuccess = function (file) {
var reader = new FileReader();
reader.onloadend = function (evt) {
//promise is resolved with the base64 string
deferred.resolve(evt.target.result);
};
reader.readAsDataURL(file);
};
window.resolveLocalFileSystemURL(fileUrl, fileResolved, fail);
return deferred.promise;
} readAsDataURL方法用于读取指定的Blob或File的内容。当读取操作完成后,readyState就完成了,加载端是triggered.At,此时,结果属性包含数据作为base64编码字符串来表示文件的数据。
用法:
var imageData;
getContentAsBase64(aq.FileUri).then(function (content) {
imageData= content;
});https://stackoverflow.com/questions/9583363
复制相似问题