|

楼主 |
发表于 2016-12-5 18:16:21
|
显示全部楼层
不是image,是pdf,word等文件,我用open打开但提示文件不存在,但是下载成功的回调函数是有值的。- var row = event.bindingContext.$object;
- var filePath = cordova.file.documentsDirectory +"www/"+ row.val('fFileName');
-
- var fileName = row.val('fFileName');
- var divProgress = $(event.source.domNode.parentElement.parentElement).find("div[xid='progressBar1']"); //获取当前行的进度条XID
- var progress = $(event.source.domNode.parentElement.parentElement).find("div[xid='progress1']");
- progress.removeClass("hide");
- var uri = encodeURI(row.val("fDownloadPath"));
- justep.Util.hint({"text":'正在下载……',"delay":1000});
- var fileTransfer = new FileTransfer();
- fileTransfer.onprogress = function(progressEvent) { //文件传输插件对象的进度方法调用
- var percentProgress = parseInt((progressEvent.loaded / progressEvent.total) * 100)+ "%"; //获取已下载和总大小的百分比
- if (progressEvent.lengthComputable)//获取浏览器返回HEAD信息包含CONTENT-LENGTH为TRUE 则执行下面
- {
- divProgress.css("width",percentProgress); //设置进度条的样式
- divProgress.html(percentProgress); //设置进度条里面的数字
- }
- };
- fileTransfer.download( //调用对象的下载方法,开始下载
- uri,
- filePath,
- function(entry) {
- alert(entry.nativeURL);
- var index= fileName.indexOf(".");
- var file_id =fileName.substring(index);
- //alert(file_id);
- //保存至相册
- if(file_id ==".BMP"||file_id ==".PNG" || file_id ==".GIF" || file_id ==".JPG" || file_id ==".JPEG"||file_id ==".bmp"||file_id ==".png" || file_id ==".gif" || file_id ==".jpg" || file_id ==".jpeg"){
- fileTransfer.saveToAlbum(
- filePath,
- function successCallback() {
- justep.Util.hint({"text":'已保存至相册',"delay":1000});
- cordova.plugins.fileOpener2.open(
- filePath,
- "image/jpeg",
- {
- error : function(){ },
- success : function(){
- justep.Util.hint({"text":'正在打开...',"delay":1000});
- }
- }
- );
- },
- function errorCallback() {
- justep.Util.hint({"text":'保存失败',"delay":1000,"type":"danger"});
- }
- );
- }else if(file_id ==".pdf"||file_id ==".PDF"){//打开文件代码
-
- cordova.plugins.fileOpener2.open(
- encodeURI(filePath),
- "application/pdf",
- {
- error : function(e){
- justep.Util.hint('Error status: ' + e.status + ' - Error message: ' + e.message);
- },
- success : function(){
- justep.Util.hint({"text":'正在打开...',"delay":1000});
- }
- }
- );
- }else if(file_id ==".doc"||file_id ==".docx"||file_id ==".xls"||file_id ==".xlsx"||file_id ==".ppt"||file_id ==".pptx"){
- cordova.plugins.fileOpener2.open(
- filePath,
- "application/msword",
- {
- error : function(e){
- justep.Util.hint('Error status: ' + e.status + ' - Error message: ' + e.message);
- },
- success : function(){
- justep.Util.hint({"text":'正在打开...',"delay":1000});
- }
- }
- );
-
- }
- /* downLoadBtn.hide();*/
- },
- function(error) { //出错回调函数
- console.log("download error source " + error.source);
- console.log("download error target " + error.target);
- console.log("upload error code" + error.code);
- justep.Util.hint({"text":'下载失败',"delay":1000});
- },
- false,
- {
- headers: {
- "Authorization": "Basic dGVzdHVzZXJuYW1lOnRlc3RwYXNzd29yZA=="
- }
- }
- );
复制代码 这是我下载时的代码,我是想和微信打开doc文档一样打开在服务器上下载的doc文档。 |
|