zoukankan      html  css  js  c++  java
  • ionic cordova file download and load

    1.先添加插件

    cordova plugin add org.apache.cordova.file
    cordova plugin add org.apache.cordova.file-transfer 
    

      2. 在index.html 中添加

    <img width="80px" height="80px" id="smallImage" />
    

      3.在index.js中添加

    document.addEventListener("deviceready", downloadstart, false);
    /**
     * 用一個文件夾download,存儲下載的圖片/文件 ;
     * @return {[type]} [description]
     */
    function downloadstart() {
        try {
            window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSystem) {
                fileSystem.root.getDirectory("download", {
                    create: true,
                    exclusive: false
                }, function(entry) {
                    var storepath = entry.toURL();
                    alert(storepath.toString() + '文件夹已經存在');
                    downloadFile(storepath, "http://www.baidu.com/img/bdlogo.png");//開始下載文件
                }, function() {
                    console.log('创建文件夹失败');
                });
            }, function() {
                console.log('创建文件夹失败');
            });
        } catch (e) {
            alert(e.name + ":" + e.message);
        }
    }
    /**
     * 下載文件,
     *
     * @param  {[string]} storefilepath   [下載的文件存儲的路徑]
     * @param  {[string]} downloadfileurl [需要下載文件的URL 或者名稱]
     * @return {[type]}                 [description]
     */
    function downloadFile(storefilepath, downloadfileurl) {
        try {
            var ft = new FileTransfer();
            var uri = encodeURI(downloadfileurl);
            var fileURL = storefilepath + downloadfileurl.substr(downloadfileurl.lastIndexOf('/') + 1);
            ft.download(
                uri,
                fileURL,
                function(entry) {
                    var smallImage = document.getElementById('smallImage');
                    smallImage.style.display = 'block';
                    smallImage.src = entry.toURL();
                    alert("download complete: " + entry.toURL());
                },
                function(error) {
                    alert("download error source " + error.source);
                    alert("download error target " + error.target);
                    console.log("download error source " + error.source);
                    console.log("download error target " + error.target);
                    console.log("upload error code" + error.code);
                },
                false, {
                    headers: {
                        "Authorization": "Basic dGVzdHVzZXJuYW1lOnRlc3RwYXNzd29yZA=="
                    }
                }
            );
        } catch (e) {
            alert(e.name + ":" + e.message);
        }
    }
    

      

  • 相关阅读:
    用webview打开网页时,里面有个div带滚动条的,但是在平板上滚动条失效
    PHP获取日期
    iScroll滚动区域中select、input、textarea元素无法点击的Bug修复
    用PHP获取系统时间时,时间比当前时间少8个小时
    kindeditor-4.1.7应用
    PHP连接SQLServer
    PHP常见方法
    APPLICATION ERROR #1502 .
    JSP 简介
    转发和重定向的区别与联系
  • 原文地址:https://www.cnblogs.com/xieyier/p/4014235.html
Copyright © 2011-2022 走看看