//重传;合并错误的文件删掉重新上传
reUpload(file, queueId) {
//获取文件对象
let that = this;
var total = file.size;//文件二进制总大小
var filename = file.name;
var identifier = filename + total + that.userId;//此文件的唯一标识(非块)
api.appReupload(queueId, identifier)
.then(res => {
console.log(res)
api.showVuxToast(that, res.data.message);
if (res.data && res.data.status) {
//重传准备完毕,可以开始上传
that.clickUploadBtn();
}
})
.catch(e => {
api.showVuxToast(that, "请求出错", "cancel");
})
},
//点击重传按钮
reUploadClick(id) {
this.stop = false;
this.switchUploadAndStop = false;
this.recentPerText = "重传准备中.."
let that = this;
let photoInfo = "";
for (let index in that.photoList) {
photoInfo = that.photoList[index];
if (photoInfo.id == id) {
break;
}
}
let path = photoInfo.path;// 通过路径获得文件
console.log("文件的全路径为==" + path);
if (window.LocalFileSystem != undefined) {//获得权限
let fileDir = path.substring(0, path.lastIndexOf("/"));
let fileName = path.substring(path.lastIndexOf("/") + 1);
window.resolveLocalFileSystemURL(fileDir, function (dirEntry) {//先获得路径文件夹,,
console.log("找到了文件所在路径=" + dirEntry.toURL())
dirEntry.getFile(fileName, {create: false}, function (fileEntry) {//读取文件
console.log("读取到了文件");
fileEntry.file(function (file) {//开始分块上传
console.log("调用重传方法")
that.reUpload(file, id);
}, onErrorReadFile);
}, onErrorGetFile)
}, onErrorLoadFs);
} else {
console.log("环境不支持")
}
function onErrorReadFile(msg) {
that.$vux.toast.text("读取文件失败");
console.log("读取文件失败=" + msg.toString())
}
function onErrorLoadFs(msg) {
that.$vux.toast.text("读取目录失败");
console.log("读取目录失败=" + msg.toString())
}
function onErrorGetFile(msg) {
that.$vux.toast.text("未找到文件");
console.log("未找到文件=" + msg.toString())
}
},
//计算进度
callPercent(cuLoaded, total) {
let percent = Math.round((cuLoaded / total) * 100);//计算上传百分比
if (this.photoList[0]) {
this.photoList[0].percent = percent + "%";
this.totalCount = this.photoList.length;
this.recentPercent = percent;//必须给外面一个赋值,不然数值渲染不上去
} else {
this.totalCount = 0;
this.recentPercent = percent;
this.recentPerText = "";
}
},
//点击暂停按钮
stopUploadClick() {
this.stop = true;
this.switchUploadAndStop = true;
this.recentPerText = "正在暂停"
},
//点击上传按钮(仅支持单个文件上传)
clickUploadBtn() {
this.stop = false;
this.switchUploadAndStop = false;
this.recentPerText = "准备上传中.."
this.uploadClick();
},
//上传(仅支持单个文件上传)
uploadClick() {
let that = this;
//获取第一个文件
that.photoInfo = that.photoList[0];
if (that.photoInfo) {
if (Vue.cordova.device.platform === "Android") {//是安卓的
//控制按钮显示隐藏
that.switchUploadAndStop = false;
// 通过路径获得文件
let path = that.photoInfo.path;
//获得权限
if (window.LocalFileSystem != undefined) {
let fileDir = path.substring(0, path.lastIndexOf("/"));
let fileName = path.substring(path.lastIndexOf("/") + 1);
//先获得路径文件夹,,
window.resolveLocalFileSystemURL(fileDir, function (dirEntry) {
//在当前文件夹里获得这个名字的文件
dirEntry.filesystem.root.getFile(fileName, {create: false}, function (fileEntry) {
//读取文件
fileEntry.file(function (file) {
//开始分块上传
that.toChunkUpload(file, path);
}, onErrorReadFile);
}, onErrorGetFile)
}, onErrorLoadFs);
} else {
console.log("环境不支持")
}
} else {//ios
that.switchUploadAndStop = false;//控制上传和暂停按钮显示隐藏
let path = that.photoInfo.path;// 通过路径获得文件
console.log("文件的全路径为==" + path);
if (window.LocalFileSystem != undefined) {//获得权限
let fileDir = path.substring(0, path.lastIndexOf("/"));
let fileName = path.substring(path.lastIndexOf("/") + 1);
window.resolveLocalFileSystemURL(fileDir, function (dirEntry) {//先获得路径文件夹,,
console.log("找到了文件所在路径=" + dirEntry.toURL())
dirEntry.getFile(fileName, {create: false}, function (fileEntry) {//读取文件
console.log("读