1.先添加插件html
cordova plugin add org.apache.cordova.file cordova plugin add org.apache.cordova.file-transfer
2. 在index.html 中添加apache
<img width="80px" height="80px" id="smallImage" />
3.在index.js中添加url
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); } }