zoukankan      html  css  js  c++  java
  • 附件下载并查看方法

    用法:

      var attachments = res.attachments[0];   //拿到附件的一些参数
     
                var download = function(attachments) {          //定义的一个下载附件方法
                    if (attachments && !attachments.attachmentUrl) {
                      return;
                    }
                    console.log('window.savePath',window.savePath)
                    var docuementName = attachments.attachmentName;
                    var filePath = window.savePath + "/" + docuementName;
                    console.log('下载处的filepath',filePath)
                    var url = attachments.attachmentUrl;
                    var fileTransfer = new FileTransfer();
                    fileTransfer.download(
                      url,
                      filePath,
                      function(entry) {
                        //将文件路径保存起来,方便后面调用
                        window.fileUri = entry.fullPath.replace("file:///", "file://");
                        var URL = window.fileUri;
                        app.openFile(URL, "", function(res) {
                          app.hint("打开成功!");
                        });
                      },
                      function(error) {
                        console.log('打开失败')
                        app.hint("下载失败!");
                      }
                    );
                  };
     
                app.getAppDirectoryEntry(function (res) {      //获取手机保存的文件路径
                    if (window.devicePlatform == "android") {
                        window.savePath = res.sdcard;
                    } else if (window.devicePlatform == "iOS") {
                        window.savePath = res.documents;
                    }
                    download(attachments);  //调用方法
                }); 
     
     
    下载后打开文件方法openfile,        调用   bingotouch.js里面的方法
        /**
         *  打开文件:如office文件,apk等,将调用系统的程序来打开文件
         *  @method app.openFile
         *  @param filePath {String} 文件地址
         *  @param mime {String} mime类型
         *  @param success {Function} 打开成功回调
         *  @param fail {Function} 打开失败回调
         *  @example
         *  app.openFile("file:///mnt/sdcard/bingotouch.docx","docx",function(res){
         *      app.hint("打开成功!");
         *  });
        */
       app.openFile = function(filePath, mime, success, fail) {
            filePath = filePath || "";
            mime = mime || "";
            success = success || function(result) {};
            fail = fail || function(result) {
                app.hint("没有找到合适的程序打开该文件");
            };
            if (window.devicePlatform == "iOS") {
                //ios无需带上file
                filePath = filePath.replace("file://","");
            }
            Cordova.exec(success, fail, "ExtendApp", "openFile",[ filePath, mime ]);
        }
  • 相关阅读:
    Google I/O 官方应用中的动效设计
    浪院长 | spark streaming的使用心得
    Kubernetes 1.12公布:Kubelet TLS Bootstrap与Azure虚拟机规模集(VMSS)迎来通用版本号
    安卓自己定义View进阶-Path基本操作
    2014编程之美初赛第二场
    2015年,即将结束
    查看JVM运行时参数
    使用ThreadPoolExecutor线程池实现并发操作并返回结果
    mysql数据库将查询的多条结果的某些字段合并为一个字段处理
    mysql数据库使用mybatis新增操作返回自增主键的值
  • 原文地址:https://www.cnblogs.com/chchchc/p/12092091.html
Copyright © 2011-2022 走看看