<template>
<view class="page">
<progress percent="80" active />
{{GethasLogin}}
<view class="title">
{{title}}
</view>
<view>{{msg}}</view>
<button type="primary" @tap="start">start upload</button>
<button type="primary" @tap="pause">pause upload</button>
<button type="primary" @tap="resume">resume upload</button>
<button type="primary" @tap="abort">abort upload</button>
<view class="page-body">
<view class="btn-area">
<navigator url="/pages/API/action-sheet/action-sheet" hover-class="navigator-hover">
<button type="default">ction-sheet</button>
</navigator>
</view>
</view>
</view>
</template>
<script>
import {
mapGetters
} from 'vuex';
export default {
data() {
return {
title: `this is test tabbar`,
msg: '',
path: '',
task: null
}
},
onShow: function() {
console.log(`test onLoad page...`);
},
methods: {
start(){
this.createUpload(this.path);
},
pause(){
console.log('===pause===', JSON.stringify(this.task));
this.msg = '上传暂停';
this.task.pause();
},
resume(){
this.msg = '继续上传中';
this.task.resume();
},
abort(){
this.msg = '取消上传';
this.task.abort();
},
previewImage() {
plus.nativeUI.previewImage([
"http://img-cdn-qiniu.dcloud.net.cn/icon1.png",
"http://img-cdn-qiniu.dcloud.net.cn/icon2.png",
"http://img-cdn-qiniu.dcloud.net.cn/icon3.png"
]);
},
createUpload(path) {
let that = this;
// plus.nativeUI.showWaiting( "上传中..." );
that.msg = '上传中';
that.task = plus.uploader.createUpload("http://192.168.1.141:7021/xinglan-web/uploadFile/upload", {
method: "POST",
blocksize: 1048576
}, function(t, status) {
console.log(JSON.stringify(t), t.responseText.message);
// 上传完成
if (status == 200) {
plus.nativeUI.alert("Upload success: " + t.url);
that.msg = '上传完成';
// plus.nativeUI.closeWaiting();
} else {
plus.nativeUI.alert("Upload failed: " + status);
console.log('error==', status);
that.msg = '上传出错';
}
}
);
that.task.addFile(path, {
key: "uploadFile"
});
that.task.addData( "string_key", "string_value");
that.task.addEventListener( "statechanged", function(upload, status){
console.log('==statechanged==', upload.state)
console.log('===status===', status);
}, false );
that.task.start();
},
chooseFile() {
uni.chooseImage({
success: (chooseImageRes) => {
plus.nativeUI.alert(chooseImageRes.tempFilePaths[0]);
uni.setStorageSync('storage_key','uuid123456');
plus.nativeUI.alert('storage_key='+uni.getStorageSync('storage_key'));
}
});
}
},
onNavigationBarButtonTap(e) {
let that = this;
// 监听原生标题栏按钮点击事件,参数为Object
this.title = e.index;
// that.chooseFile();
// 单选
plus.gallery.pick(function(path) {
that.path = path;
// that.createUpload(path)
}, function(e) {
console.log("取消选择了");
that.path = '';
}, {
filter: "video"
});
//多选
// plus.gallery.pick(function(e) {
// for (var i in e.files) {
// console.log(e.files[i]);
// plus.nativeUI.alert(e.files[i]);
// }
// }, function(e) {
// console.log("取消选择图片");
// }, {
// filter: "image",
// multiple: true
// });
// this.previewImage()
}
}
</script>
<style lang="scss">
.page {
padding: 10upx;
.page-body {
// background: blue;
.btn-area {
font-size: 24upx;
}
}
}
</style>

c***@163.com
- 发布:2018-10-11 14:37
- 更新:2018-10-15 11:09
- 阅读:1935
5 个回复
Trust - 少说废话
c***@163.com (作者)
就是按照上面贴出的代码的执行方式,如果不暂停上传是可以的,但是当我点击暂停上传调用pause无效,
Trust - 少说废话
测试了一下,start 之后延时 100ms 直接调用 pause/abort 任务会暂停/结束,并提示上传失败。
你判断任务未能暂停的依据是什么,服务端依旧接收到了完整的文件?还是前端状态返回了 200,请详细描述。
c***@163.com (作者)
我是根据status 200来判断的
2018-10-11 15:05
c***@163.com (作者)
> {"UUID":"uploader331539244406623","url":"http://192.168.1.141:7021/xinglan-web/uploadFile/upload","options":{"method":"POST","blocksize":1048576},"uploadedSize":5030426,"totalSize":5030426,"responseText":"Socket closed","method":"POST","timeout":120,"retry":3,"retryInterval":30,"priority":1,"eventHandlers":{"statechanged":{"type":"statechanged","handles":[null],"capture":false}},"requestHeaders":{},"responseHeaders":null,"noParseResponseHeader":null,"cacheReponseHeaders":{},"state":4}
为什么我延时500ms后调用resume后 state的状态是4呢
DCloud_App_Array
上传实际上是不支持的暂停和恢复的(http/https协议不支持暂停及恢复)
2018-10-11 18:09
c***@163.com (作者)
你好 那如何实现这样的需求呢?暂停上传恢复上传
2018-10-12 15:32
Trust - 少说废话
目前的实现机制导致可能出现失效的情况, 后续尝试优化。