手机端下载文件及创建文件夹

            //检查文件夹是否已存在
                    var relativePath = "_downloads/zipFile";
                    plus.io.resolveLocalFileSystemURL(relativePath, function(entry) {
                        console.log("文件夹存在,地址=" + relativePath);
                        //本地相对路径("_downloads/logo.jpg")转成SD卡绝对路径("/storage/emulated/0/Android/data/io.dcloud.HBuilder/.HBuilder/downloads/logo.jpg");
                        var sd_path = plus.io.convertLocalFileSystemURL(relativePath);
                        alert("文件夹已存在路径:" + sd_path);
                        var relativePath2 = "_downloads/zipFile/111.txt";//相对路径
                       //调用下载文件
              setImgFromNet(url,relativePath2);
}, function(e) { //如果文件夹不存在,创建文件夹 console.log("创建文件夹路径=" + plus.io.convertLocalFileSystemURL(path)); plus.io.requestFileSystem(plus.io.PUBLIC_DOWNLOADS, function(success) { success.root.getDirectory(path, {create: true}, function() {//entry创建成功 console.log('文件夹创建成功') getCreateFile(sid,type,relativePath,filename); },function(e){ console.log('文件夹创建失败') }); });
            });
/*联网下载文件,loadUrl远程服务器下载路径,relativePath手机路径*/
            function setImgFromNet (loadUrl,relativePath) {
                //创建下载任务
                var sd_path = plus.io.convertLocalFileSystemURL(relativePath);//得出手机的物理路径
                var dtask = plus.downloader.createDownload(loadUrl, {method: "GET",filename:sd_path,timeout: 10}, function(d, status) {//filename自定义文件下载的路径(包含文件名)
                    if (status == 200) {
                        //下载成功
                        console.log("下载成功=" + relativePath);
                    } else {
                        //下载失败,需删除本地临时文件,否则下次进来时会检查到图片已存在
                        console.log("下载失败=" + status+"=="+sd_path);
                        //dtask.abort();//文档描述:取消下载,删除临时文件;(但经测试临时文件没有删除,故使用delFile()方法删除);
                    }
                });
                //启动下载任务
                dtask.start();
            }

参考地址路径:https://blog.csdn.net/swingyaoye/article/details/50403752

主要解决将下载后的文件保存到手机本地(物理)路径。

下面这个解决手机端创建空文件夹;手机端下载文件的话可以调用上面,“联网下载指定路径”直接下载文件。

              var path = _downloads+"/"+ filename;
                        console.log("文件路径=" + plus.io.convertLocalFileSystemURL(path));//创建手机文件的物理路径
                        plus.io.requestFileSystem(plus.io.PUBLIC_DOWNLOADS, function(success) {
                            success.root.getDirectory(path, {create: true}, function(entry) {
                                /*entry.createWriter(
                                    function(suc) {
                                    
                                    },
                                    function(err){
                                    console.log("写入失败")
                                    }
                                )*/
                            },function(e){
                            console.log('文件夹创建出错')
                            });
                            
                            var filepath = path+"/111.txt"; 
                            success.root.getFile(path, {create: true}, function(entry) {
                                /*entry.createWriter(
                                    function(suc) {
                                    
                                    },
                                    function(err){
                                    console.log("写入失败")
                                    }
                                )*/
                            },function(e){
                            console.log('文件创建出错')
                            });
                        });

参考地址:http://www.webkfa.com/one4/w1303.html

原文地址:https://www.cnblogs.com/mangwusuozhi/p/10929594.html