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 ]);
        }
  • 相关阅读:
    解决Win8设置为快速启动后ubuntu不能访问win下磁盘的方法
    Why Not Specialize Function Templates?
    Build your first web service with PHP, JSON and MySql
    本地开发与linux服务器端的文件传输
    数据库设计中一对多的解决方法.
    C++ 模板练习摘要
    Understanding ASP.NET Validation Techniques
    我的sublime text 个人设置
    Eclipse 快捷键
    数字图像处理
  • 原文地址:https://www.cnblogs.com/chchchc/p/12092091.html
Copyright © 2011-2022 走看看