-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjs文件点击下载,文件上传,获取格式化时间.txt
83 lines (80 loc) · 2.78 KB
/
js文件点击下载,文件上传,获取格式化时间.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
downLoadSvgFile() { // 点击下载文件
let myDate = Date.now();
let svgFile = document.getElementById("svgContainer").getElementsByTagName("svg")[0];
var link = document.createElement('a');
link.style.display = 'none';
document.body.appendChild(link);
var blob = new Blob([this.svgToString(svgFile)], {type: 'text/plain'});
link.href = URL.createObjectURL(blob);
if (this.fileName.includes(".svg")) {
link.download = myDate + "_" + this.fileName;
} else {
link.download = myDate + "_" + this.fileName + '.svg';
}
link.click();
},
================
svg 保存
svgToString(svg) {
let viewBox = svg.getAttribute("viewBox")
return [
'<?xml version="1.0" encoding="UTF-8"?>\n',
'<svg xmlns="http://www.w3.org/2000/svg" viewBox="' + viewBox + '">\n', //viewBox="0 0 600 400"
svg.innerHTML,
'</svg>'
].join('');
},
======================
//上传文件到服务器上
uploadFile() {
let _that = this;
let myDate = this.getTime();
let svgFile = document.getElementById("svgContainer").getElementsByTagName("svg")[0];
var blob = new Blob([this.svgToString(svgFile)], {type: 'text/plain'});
let fileName = "" + myDate + "/" + _that.fileName
let file = new window.File([blob], fileName)
let params = new FormData();
params.append('file', file);
if (!file) {
this.messageBox('请上传文件');
return;
}
UploadFile('/svgFileUpload', params, "svgURI").then((res) => {
if (res.code === 200) {
_that.printContent("返回文件路径:", res)
let uploadFilePath = res.path;
_that.messageBox("文件保存成功", 'success')
} else {
_that.$message.error(res.msg);
}
}).catch(e => {
_that.$message.error(e.message);
})
},
======================
getTime() {
var date = new Date();
var year = date.getFullYear();
var month = date.getMonth() + 1;
var d = date.getDate();
var hour = date.getHours();
var minute = date.getMinutes();
var second = date.getSeconds();
if (month < 10) {
month = "0" + month;
}
if (d < 10) {
d = "0" + d;
}
if (hour < 10) {
hour = "0" + hour;
}
if (minute < 10) {
minute = "0" + hour;
}
if (second < 10) {
second = "0" + second;
}
return year + "" + month + "" + d + "" + hour + "" + minute + "" + second;
},
======================