下载文件时,在ios上运行项目是没有问题的,直接就下载了,,可以打开。
但是在安卓上面会出现下载不下来,plus.downloader.createDownload回调state的值是2,然后输出404了。部分安卓机器可以下载下来但是下载不完全。
代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
<title></title>
<script src="js/mui.min.js"></script>
<link href="css/mui.min.css" rel="stylesheet" />
</head>
<body>
<header class="mui-bar mui-bar-nav" style="padding-right: 15px;background-color: #00BFFF;">
<a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left"></a>
<h1 class="mui-title" style="color: white;">通知公告</h1>
</header>
<div class="mui-content">
<ul class="mui-table-view">
<li class="mui-table-view-cell">
<label>标题:</label>
<span id="title"></span>
</li>
<li class="mui-table-view-cell">
<label>发布人:</label>
<span id="creater"></span>
</li>
<li class="mui-table-view-cell">
<label>发布时间:</label>
<span id="creater_time"></span>
</li>
<li class="mui-table-view-cell">
<label>收件人:</label>
<span id="addressee"></span>
</li>
</ul>
<div class="mui-table-view-cell" id='attc'>
<label>附件:</label>
</div>
<div class="mui-content">
<div class="mui-content-padded" id="content">
</div>
</div>
</div>
<script src="js/jquery-1.12.4.min.js"></script>
<script type="text/javascript" charset="UTF-8">
mui.init({
swipeBack: true
});
if (window.plus) {
plusReady();
} else {
document.addEventListener("plusready", plusReady, false);
}
var dtask = null;
function plusReady() {
var self = plus.webview.currentWebview();
var name = self.name;
var item = JSON.parse(name);
var user = JSON.parse(localStorage.user);
var server = 'http://116.228.62.2:88/Admin/oaApp/EmpNo.ashx';
$.get({
url: server,
data: {
empno: user[0].EmpNo
},
success: function(data) {
var date = new Date(item.time);
var attc = document.getElementById('attc')
document.getElementById('creater').innerHTML =data;
document.getElementById('title').innerHTML = item.Title;
document.getElementById('addressee').innerHTML = item.UserName
document.getElementById('creater_time').innerHTML = formatDateTime(date);
var file = item.affix.split(',');
mui.each(file, function(i, n) {
var a = document.createElement('span');
a.innerHTML = "<br><a tr=" + n + ">" + n + "</a>";
a.addEventListener('tap', function(e) {
var name = $(e.target).attr('tr');
var sd_path = "";
var watiting = plus.nativeUI.showWaiting("开始下载:0%");
var serverurl = "http://116.228.62.2:88/Files/Infomation/"+name+"";
dtask = plus.downloader.createDownload(serverurl, {
method:"GET",
retry: 0
});
dtask.addEventListener("statechanged", function(task, status) {
if (!dtask) {
return;
}
switch (task.state) {
case 1:
// 开始
break;
case 2:
// 已连接到服务器
break;
case 3:
// 已接收到数据
var nowData = Math.floor(task.downloadedSize * 100 / task.totalSize);
console.log(nowData);
if (nowData % 10 === 0) {
watiting.setTitle("已下载:" + nowData + "%");
if (nowData === 100) {
plus.nativeUI.toast("正在准备环境,请稍后!");
plus.io.resolveLocalFileSystemURL(dtask.filename, function(entry) {
sd_path = plus.io.convertLocalFileSystemURL("_downloads/" + name);
watiting.close();
plus.runtime.openFile(dtask.filename,{},function(err){
mui.alert('打开失败');
});
});
//plus.runtime.quit();
}
}
break;
case 4:
// 下载完成
break;
}
},false);
dtask.setRequestHeader('Access-Control-Allow-Origin','*');
dtask.start();
});
attc.appendChild(a);
});
document.getElementById('content').innerHTML = item.InfoContent;
},
error: function(err) {
console.log('err',err);
}
});
}
var formatDateTime = function(date) {
var y = date.getFullYear();
var m = date.getMonth() + 1;
m = m < 10 ? ('0' + m) : m;
var d = date.getDate();
d = d < 10 ? ('0' + d) : d;
var h = date.getHours();
var minute = date.getMinutes();
minute = minute < 10 ? ('0' + minute) : minute;
return y + '-' + m + '-' + d + ' ' + h + ':' + minute;
};
</script>
</body>
</html>
5 个回复
赵梦欢 - 专注前端,乐于分享!
经过排查,将代码中的错误改了以后还是会出现这种问题,而且是部分文件下载失败,目前可以判断是服务器端返回的问题。
h***@163.com
怎么解决 我也碰到这个问题了
梁爽 (作者)
安卓直接写的a标签,连接服务器地址。然后直接下载了。ios的话连接服务地址就直接打开了文件
2016-08-25 17:50
lindsy
我跟你相反的?我是安卓(oppo11)能下,state 0->2->3->4,ios(i7)下不了,state 0->卡n久->4
6***@qq.com
你好,解决了么?我也遇到这种情况
2018-09-25 14:08
l***@163.com
您好解决了吗,我也遇到这种情况了
2018-12-28 08:41
w***@qq.com - 开发
怎么解决?
1***@qq.com
怎么搞的啊?