【问题标题】:ionic framework windows phone, image/pdf file download and store it in phone memory离子框架windows手机,图片/pdf文件下载并存储在手机内存中
【发布时间】:2016-02-10 05:44:59
【问题描述】:

我们使用 ionic frameworkandroidios 和 windows 创建一个混合应用。

因为我们有文件下载并将其存储在手机/sd卡内存中的要求。

基本上,我们从 restApi 获取 base64 数据,并将其转换为 blob 数组。像这个例子Cordova-Android-Camera: How to save base64 encoded jpg to Android file system

我们能够将base64 转换为blob 并在android 和ios 中使用此代码FileWriter.write(blob) 保存文件。

但是 windows phone 我认为它不支持 blob 转换,并且在这个链接 https://github.com/apache/cordova-plugin-file 他们提到 cordova-plugin-file 不支持 FileReader.readAsArrayBuffer 也不支持 FileWriter.write(blob)。

有什么方法可以让windows phone 将base64 转成文件。

我们也尝试直接从某个链接保存图像,但我认为我们需要写权限才能在 Windows 中存储文件,我们不知道在哪里设置 Windows Phone 的写权限。

【问题讨论】:

标签: android cordova windows-phone-8 ionic-framework cross-platform


【解决方案1】:

我已经在我的一个应用程序中这样做了,它运行良好。请执行以下操作:

用js写代码:

if(window.FileTransfer){
            try{
                console.log("filesys " + filesys);
                console.log("filesys root " + filesys.root);
                console.log("filesystem root path " + filesys.getRootPath);
                var filePath = filesys.root().toURL() + "/" + fileName;
                console.log("downloading file " + JSON.stringify(remoteData) + " at " + filePath);

                var fileTransfer = new FileTransfer();
                fileTransfer.download(
                    uri,
                    filePath,
                    function(entry) {
                        var retEntry = function(entry){
                            device.hideSpinner();
                            console.log("file downloaded " + entry.toURL());
                            success(entry);
                        }

                        retEntry(entry);
                    },
                    function(error) {
                        device.hideSpinner();
                        console.log("download error source " + error.source);
                        console.log("download error target " + error.target);
                        console.log("upload error code" + error.code);
                        error(error);
                    }
                );
            }catch(e){
                device.hideSpinner();
                device.alert("error " + e);
            }           }else{
            device.hideSpinner();

            console.log("opening the file");
            error();
            window.open(uri, '_blank', 'location=yes');             }

这是正常的文件下载代码。

请安装以下插件以使其正常工作。

1) cordova-plugin-file "文件" https://github.com/apache/cordova-plugin-file

2) cordova-plugin-file-transfer "文件传输" https://github.com/apache/cordova-plugin-file-transfer

3) cordova-plugin-inappbrowser "InAppBrowser" https://github.com/apache/cordova-plugin-inappbrowser

希望对你有所帮助。

【讨论】:

  • 嗨@Naitik,我们是否需要为windows phone 设置写入权限才能保存文件。我试过你的代码,但对我来说它不起作用。
  • 你安装了我在回答中提到的所有插件吗?你得到什么样的错误?请简要描述您遇到的问题。
  • 关于您需要添加的权限:Properties/WPAppManifest.xml 文件,如下所示:
  • 嗨@Naitik 谢谢你,我们解决了这个问题,在windows设备文件正在保存,文件保存后我们使用$cordovaFileOpener2(ng-cordova)插件打开保存的文件,但它正在搜索走错路了,我们修好了。
  • 如果您对我的答案有所帮助,您可以接受它作为答案,以便其他人也可以提供帮助。谢谢你。很高兴您发现问题并自行解决。
猜你喜欢
  • 2019-06-09
  • 2020-10-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-23
  • 2013-07-14
  • 2020-08-13
  • 2019-10-08
相关资源
最近更新 更多