保存dataURL的(Base64)到PhoneGap的文件(机器人) [英] Saving dataURL(base64) to file on PhoneGap (android)

查看:760
本文介绍了保存dataURL的(Base64)到PhoneGap的文件(机器人)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我转换画布dataURL的(Base64)型,我想使用的PhoneGap的作家,但将其保存到手机的文件系统没有成功(我得到坏了文件,我无法打开) - 这里是我的一些code的:

I'm converting canvas to dataURL(base64) type and I wanted to save it to phone filesystem using PhoneGap's writer but without success (I get broken file which I cannot open) - here's some of my code:

        var dataURL = document.getElementById("gen").toDataURL('image/png'); //substr() .replace('datadata:image/png;base64,', '');

         window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
         function gotFS(fileSystem) {
            fileSystem.root.getFile("screenshot.png", {create: true, exclusive: false}, gotFileEntry, fail);
        }

        function gotFileEntry(fileEntry) {
            fileEntry.createWriter(gotFileWriter, fail);
        }

        function gotFileWriter(writer) {
            console.log("open and write");
            writer.seek(0);
            writer.write(dataURL);
            console.log("close and save");
        }

        function fail(error) {
            console.log(error.code);
        }

        var fileTransfer = new FileTransfer();
        fileTransfer.download("/", screenshot.png,  
            function(entry) {
                alert("download complete");
            },
            function(error) {
                alert("download error source " + error.source);
                alert("download error target " + error.target);
                alert("upload error code" + error.code);
            }
        );

我也尝试从计算器,这是基于addtional Java插件等解决方案,但它并没有为我工作。是否有纯JS(额外的JS库)解决方案呢?

I've tried also other solution from stackoverflow, which was based on addtional java plugin but it hasn't work for me. Is there pure JS(with additional js libs) solution for it?

推荐答案

您可能需要请求更大的文件配额是大到足以容纳映像的文件系统。默认值通常是5 MB,但因为它不是标化在不同的浏览器可能会有所不同。

You may need to request larger file quota for the file system that is big enough to hold the image. Default is typically 5 mb but may vary in the different browsers as it's not standarized.

如果数据-URI的长度超过这个(这很可能是考虑到它是一个PNG文件与33%增加33%的开销为base64),该文件将不会保存正确。

If the length of the data-uri exceeds this (which is likely considering it's a PNG file with 33% added 33% overhead as base64) the file won't save properly.

您可以使用配额API请求限额:

You can request quota using the quota API:

webkitStorageInfo.requestQuota( 
    webkitStorageInfo.PERSISTENT
    newQuotaInBytes,
    quotaCallback,
    errorCallback);

更多细节可以发现的这里

这篇关于保存dataURL的(Base64)到PhoneGap的文件(机器人)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆