联系信息
更新3日志
class="upload-demo" action="" ref="upload" :file-list="fileList" :limit="1" :on-change="handleChange" :on-remove="handleRemove" :auto-upload="false" accept=".zip,.rar" > >选取文件 >
style="margin-left: 10px" size="small" type="success" @click="submitForm" >上传到服务器
>
style="margin-left: 10px" size="small" type="success" @click="resumeUpload" >继续
>
style="margin-left: 10px" size="small" type="success" @click="stopUplosd" >暂停
>
style="margin-left: 10px" size="small" type="success" @click="abortMultipartUpload" >清除切片
>
:percentage="percentage" :status="uploadStatus" >
捐赠支持
import OSS from "ali-oss";
import { getSts } from "@/api/tool/upload";
// let OSS = require("ali-oss"); // 引入ali-oss插件
import { ElMessage } from "element-plus";
const client = ref();
const version = ref("3.3.0");
const fileList = ref([]);
const file = ref(null);
const tempCheckpoint = ref(null); // 用来缓存当前切片内容
const uploadId = ref("");
const uploadStatus = ref(null); // 进度条上传状态
const percentage = ref(0); // 进度条百分比
const uploadName = ref("");
function goTarget(url) {
window.open(url, "__blank");
}
// 点击上传至服务器
async function submitForm(file) {
const res = await getSts({});
console.log(res);
client.value = await new OSS({
region: "oss-cn-*******", //根据那你的Bucket地点来填写
accessKeyId: res.data.accessKeyId,
accessKeySecret: res.data.accessKeySecret,
bucket: "********************", //bucket名字
stsToken: res.data.securityToken,
});
multipartUpload();
}
// 取消分片上传事件
async function abortMultipartUpload() {
window.removeEventListener("online", resumeUpload);
const name = uploadName.value; // Object所在Bucket的完整路径。
const uploadId = uploadId.value; // 分片上传uploadId。
const result = await client.abortMultipartUpload(name, uploadId);
console.log(result, "=======清除切片====");
}
// 暂停分片上传。
function stopUplosd() {
window.removeEventListener("online", resumeUpload); // 暂停时清除时间监听
let result = client.value.cancel();
console.log(result, "---------暂停上传-----------");
}
// 切片上传
async function multipartUpload() {
if (!file.value) {
ElMessage.error("请选择文件");
return;
}
uploadStatus.value = null;
// console.log("this.uploadStatus",this.file, this.uploadStatus);
console.log(client.value);
percentage.value = 0;
try {
//object-name可以自定义为文件名(例如file.txt)或目录(例如abc/test/file.txt)的形式,实现将文件上传至当前Bucket或Bucket下的指定目录。
let result = await client.value.multipartUpload(
file.value.name,
file.value,
{
headers: {
"Content-Disposition": "inline",
"Content-Type": file.value.type, //注意:根据图片或者文件的后缀来设置,我试验用的‘.png’的图片,具体为什么下文解释
},
progress: (p, checkpoint) => {
tempCheckpoint.value = checkpoint;
uploadId.value = checkpoint.uploadId;
uploadName.value = checkpoint.name;
percentage.value = p * 100;
console.log(
p,
checkpoint.value,
percentage.value,
"---------uploadId-----------"
);
// 断点记录点。浏览器重启后无法直接继续上传,您需要手动触发上传操作。
},
parallel: 4,
meta: { year: 2023, people: "dev" },
mime: file.value.type,
}
);
console.log(result, percentage.value, "result= 切片上传完毕=");
} catch (e) {
window.addEventListener("online", resumeUpload); // 该监听放在断网的异常处理
// 捕获超时异常。
if (e.code === "ConnectionTimeoutError") {
// 请求超时异常处理
this.uploadStatus = "exception";
console.log("TimeoutError");
// do ConnectionTimeoutError operation
}
// console.log(e)
}
}
// 恢复上传。
async function resumeUpload() {
window.removeEventListener("online", resumeUpload);
if (!tempCheckpoint.value) {
ElMessage.error("请先上传");
return;
}
uploadStatus.value = null;
try {
let result = await client.value.multipartUpload(
file.value.name,
file.value,
{
headers: {
"Content-Disposition": "inline",
"Content-Type": file.value.type, //注意:根据图片或者文件的后缀来设置,我试验用的‘.png’的图片,具体为什么下文解释
},
progress: (p, checkpoint) => {
percentage.value = p * 100;
console.log(
p,
checkpoint.value,
"checkpoint----恢复上传的切片信息-------"
);
tempCheckpoint.value = checkpoint;
},
checkpoint:tempCheckpoint.value,
meta: { year: 2020, people: "dev" },
mime: file.value.type,
}
);
console.log(result, "result-=-=-恢复上传完毕");
} catch (e) {
console.log(e, "e-=-=-");
}
}
// 选择文件发生改变
function handleChange(files, fileList) {
console.log(files, fileList);
fileList.value = fileList.filter((row) => row.uid == files.uid);
file.value = files.raw;
// 文件改变时上传
// this.submitForm(file)
}
function handleRemove(file, fileList) {
this.percentage = 0; //进度条置空
this.fileList = [];
}
.home {
.col-item {
margin-bottom: 20px;
}
ul {
padding: 0;
margin: 0;
}
font-family: "open sans", "Helvetica Neue", Helvetica, Arial, sans-serif;
font-size: 13px;
color: #676a6c;
overflow-x: hidden;
ul {
list-style-type: none;
}
h4 {
margin-top: 0px;
}
h2 {
margin-top: 10px;
font-size: 26px;
font-weight: 100;
}
p {
margin-top: 10px;
b {
font-weight: 700;
}
}
.update-log {
ol {
display: block;
list-style-type: decimal;
margin-block-start: 1em;
margin-block-end: 1em;
margin-inline-start: 0;
margin-inline-end: 0;
padding-inline-start: 40px;
}
}
}