@lambo-design/upload-file 1.0.0-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +2 -0
- package/package.json +19 -0
- package/src/index.vue +233 -0
- package/src/styles/index.less +3 -0
package/index.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@lambo-design/upload-file",
|
|
3
|
+
"version": "1.0.0-beta.1",
|
|
4
|
+
"description": "",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"author": "lambo",
|
|
7
|
+
"license": "ISC",
|
|
8
|
+
"publishConfig": {
|
|
9
|
+
"access": "public",
|
|
10
|
+
"registry": "https://registry.npmjs.org/"
|
|
11
|
+
},
|
|
12
|
+
"dependencies": {
|
|
13
|
+
"axios": "^0.24.0",
|
|
14
|
+
"axios-cache-plugin": "^0.1.0",
|
|
15
|
+
"@lambo-design/core": "4.7.1-beta.36",
|
|
16
|
+
"@lambo-design/shared": "1.0.0-beta.15"
|
|
17
|
+
},
|
|
18
|
+
"scripts": {}
|
|
19
|
+
}
|
package/src/index.vue
ADDED
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
<template>
|
|
2
|
+
<div class="upload-file-box">
|
|
3
|
+
<div class="upload-btn-box">
|
|
4
|
+
<Button :size="buttonSize" icon="md-cloud-upload" type="primary" :loading="loading"
|
|
5
|
+
@click="UploadFile()" ghost v-if="uploadBtn">
|
|
6
|
+
<span v-if="!loading">{{title}}</span>
|
|
7
|
+
<span v-else>Loading...</span>
|
|
8
|
+
</Button>
|
|
9
|
+
<a v-if="!uploadBtn" @click="UploadFile()">
|
|
10
|
+
<Icon type="md-add"></Icon>
|
|
11
|
+
上传附件
|
|
12
|
+
</a>
|
|
13
|
+
</div>
|
|
14
|
+
<input type="file" :disabled="false" ref="uploadInput" style="position:absolute; clip:rect(0 0 0 0);"
|
|
15
|
+
:multiple="multiple"
|
|
16
|
+
:accept="accept"
|
|
17
|
+
@change="readFile($event, 1)"/>
|
|
18
|
+
<div class="upload-file-list" v-if="showFileList && resultList.length > 0">
|
|
19
|
+
<List border size="small">
|
|
20
|
+
<ListItem v-for="item in resultList" :key="item.fileCode">
|
|
21
|
+
<div span="21">
|
|
22
|
+
<a :href="item.fileUrl">
|
|
23
|
+
{{item.fileName}}
|
|
24
|
+
<span class="size" style="margin-left: 5px" v-if="item.showSize">({{item.showSize}})</span>
|
|
25
|
+
</a>
|
|
26
|
+
</div>
|
|
27
|
+
<div span="3" style="text-align: right">
|
|
28
|
+
<Button @click="deleteFile(item.fileCode)" type="text">删除</Button>
|
|
29
|
+
</div>
|
|
30
|
+
</ListItem>
|
|
31
|
+
</List>
|
|
32
|
+
</div>
|
|
33
|
+
</div>
|
|
34
|
+
</template>
|
|
35
|
+
|
|
36
|
+
<script>
|
|
37
|
+
import axios from 'axios';
|
|
38
|
+
import {formatterSizeUnit} from "@lambo-design/shared/utils/date";
|
|
39
|
+
|
|
40
|
+
export default {
|
|
41
|
+
components: {},
|
|
42
|
+
name: "LamboUploadFile",
|
|
43
|
+
props: {
|
|
44
|
+
accept: {//txt,word,excel
|
|
45
|
+
type: String,
|
|
46
|
+
required: false,
|
|
47
|
+
default: "text/plain,application/msword,application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.wordprocessingml.document,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
|
|
48
|
+
},
|
|
49
|
+
fileSuffix: {
|
|
50
|
+
type: String,
|
|
51
|
+
required: false,
|
|
52
|
+
default: "pdf|doc|docx|txt|xls|xlsx|jpg|jpeg|png|gif"
|
|
53
|
+
},
|
|
54
|
+
//标题
|
|
55
|
+
title: {
|
|
56
|
+
type: String,
|
|
57
|
+
required: false,
|
|
58
|
+
default: "上传"
|
|
59
|
+
},
|
|
60
|
+
//是否多选
|
|
61
|
+
multiple: {
|
|
62
|
+
type: Boolean,
|
|
63
|
+
default: false
|
|
64
|
+
},
|
|
65
|
+
//是否按钮形式
|
|
66
|
+
uploadBtn: {
|
|
67
|
+
type: Boolean,
|
|
68
|
+
required: false,
|
|
69
|
+
default: false
|
|
70
|
+
},
|
|
71
|
+
//是否展示文件列表
|
|
72
|
+
showFileList: {
|
|
73
|
+
type: Boolean,
|
|
74
|
+
required: false,
|
|
75
|
+
default: true
|
|
76
|
+
},
|
|
77
|
+
//其他需要传递的参数
|
|
78
|
+
otherParam: {//其他需要传递的参数
|
|
79
|
+
type: String,
|
|
80
|
+
required: false,
|
|
81
|
+
default: ""
|
|
82
|
+
},
|
|
83
|
+
//文件列表
|
|
84
|
+
fileList: {
|
|
85
|
+
type: Array,
|
|
86
|
+
required: false,
|
|
87
|
+
default: () => {
|
|
88
|
+
return []
|
|
89
|
+
}
|
|
90
|
+
},
|
|
91
|
+
//上传文件上下文
|
|
92
|
+
ossServerContext: {
|
|
93
|
+
type: String,
|
|
94
|
+
default: ''
|
|
95
|
+
},
|
|
96
|
+
//上传文件Url
|
|
97
|
+
ossFilePutUrl:{
|
|
98
|
+
type: String,
|
|
99
|
+
default: "/oss/file/put"
|
|
100
|
+
},
|
|
101
|
+
//获取文件url
|
|
102
|
+
ossFileGetUrl:{
|
|
103
|
+
type:String,
|
|
104
|
+
default:"/oss/file/get/"
|
|
105
|
+
}
|
|
106
|
+
},
|
|
107
|
+
|
|
108
|
+
data() {
|
|
109
|
+
return {
|
|
110
|
+
buttonSize: 'default',
|
|
111
|
+
addFile: "",
|
|
112
|
+
loading: false,
|
|
113
|
+
attachmentFile: {
|
|
114
|
+
// fileUrl:"",
|
|
115
|
+
fileCode: "",
|
|
116
|
+
fileType: "",
|
|
117
|
+
fileName: "",
|
|
118
|
+
size: ""
|
|
119
|
+
},
|
|
120
|
+
resultList: []
|
|
121
|
+
}
|
|
122
|
+
},
|
|
123
|
+
methods: {
|
|
124
|
+
UploadFile: function () {
|
|
125
|
+
this.$refs.uploadInput.click();
|
|
126
|
+
},
|
|
127
|
+
readFile: function (e, num) {
|
|
128
|
+
let self = this;
|
|
129
|
+
//读取文件
|
|
130
|
+
// var file = e.target.files[0];
|
|
131
|
+
for (let file of e.target.files) {
|
|
132
|
+
// fileSuffix
|
|
133
|
+
var reg = new RegExp("\.(" + this.fileSuffix + ")$", "i");
|
|
134
|
+
if (!reg.test(file.name)) {
|
|
135
|
+
alert('文件类型必须是doc,docx,txt,xls,xlsx,jpg,jpeg,png中的一种');
|
|
136
|
+
return false;
|
|
137
|
+
} else {
|
|
138
|
+
self.attachmentFile.fileName = file;
|
|
139
|
+
if (self.attachmentFile.fileName.name.indexOf("/") > 0) {
|
|
140
|
+
self.attachmentFile.fileName = self.attachmentFile.fileName.name.substring(self.attachmentFile.fileName.name.lastIndexOf("/") + 1);
|
|
141
|
+
}
|
|
142
|
+
if (self.attachmentFile.fileName.name.indexOf("\\") > 0) {
|
|
143
|
+
self.attachmentFile.fileName = self.attachmentFile.fileName.name.substring(self.attachmentFile.fileName.name.lastIndexOf("\\") + 1);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
self.attachmentFile.fileType = self.attachmentFile.fileName.name.substring(self.attachmentFile.fileName.name.lastIndexOf(".") + 1);
|
|
147
|
+
|
|
148
|
+
}
|
|
149
|
+
self.loading = true;
|
|
150
|
+
self.loadtoServer();
|
|
151
|
+
},
|
|
152
|
+
loadtoServer: function () {
|
|
153
|
+
var self = this;
|
|
154
|
+
let inputDOM = this.$refs.uploadInput;
|
|
155
|
+
var afile = inputDOM.files;
|
|
156
|
+
if (afile.size == 0) {
|
|
157
|
+
alert("不能传入空文件");
|
|
158
|
+
return false;
|
|
159
|
+
}
|
|
160
|
+
let formData = new FormData();
|
|
161
|
+
for (let file of afile) {
|
|
162
|
+
formData.append('file', file);
|
|
163
|
+
}
|
|
164
|
+
let resultList = [];
|
|
165
|
+
let uploadUrl = self.ossServerContext + self.ossFilePutUrl;
|
|
166
|
+
let downloadUrl = self.ossServerContext + self.ossFileGetUrl;
|
|
167
|
+
axios.post(uploadUrl, formData).then(response => {
|
|
168
|
+
if (response.data.code == "1") {
|
|
169
|
+
var result = response.data.data;
|
|
170
|
+
self.loading = false;
|
|
171
|
+
if (result.length >= 1) {
|
|
172
|
+
result.forEach(item => {
|
|
173
|
+
let tmp = {
|
|
174
|
+
fileUrl: downloadUrl + item.fileId,
|
|
175
|
+
fileCode: item.fileId,
|
|
176
|
+
fileName: item.fileName,
|
|
177
|
+
fileType: item.fileName.substring(item.fileName.lastIndexOf(".") + 1),
|
|
178
|
+
size: item.length,
|
|
179
|
+
showSize: formatterSizeUnit(item.length),
|
|
180
|
+
otherParam: self.otherParam
|
|
181
|
+
};
|
|
182
|
+
|
|
183
|
+
resultList.push(tmp);
|
|
184
|
+
self.resultList.push(tmp);
|
|
185
|
+
});
|
|
186
|
+
if (this.showFileList) {
|
|
187
|
+
self.$emit("upload-result", self.resultList);
|
|
188
|
+
} else {
|
|
189
|
+
if (self.multiple) {
|
|
190
|
+
self.$emit("upload-result", resultList);
|
|
191
|
+
} else {
|
|
192
|
+
self.$emit("upload-result", resultList[0]);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
} else {
|
|
196
|
+
self.$emit("upload-result", null);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
self.$refs.uploadInput.value = null
|
|
200
|
+
});
|
|
201
|
+
},
|
|
202
|
+
deleteFile: function (fileCode) {
|
|
203
|
+
if (fileCode) {
|
|
204
|
+
var result = [];
|
|
205
|
+
var deletes = []
|
|
206
|
+
this.resultList.forEach(item => {
|
|
207
|
+
if (item.fileCode != fileCode) {
|
|
208
|
+
result.push(item);
|
|
209
|
+
} else {
|
|
210
|
+
deletes.push(item)
|
|
211
|
+
}
|
|
212
|
+
});
|
|
213
|
+
this.resultList = result;
|
|
214
|
+
if (this.multiple) {
|
|
215
|
+
this.$emit("upload-result", this.resultList, deletes);
|
|
216
|
+
} else {
|
|
217
|
+
this.$emit("upload-result", this.resultList, deletes);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
},
|
|
222
|
+
watch: {
|
|
223
|
+
fileList: function (value) {
|
|
224
|
+
var data = this.fileList;
|
|
225
|
+
this.resultList = data;
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
</script>
|
|
230
|
+
|
|
231
|
+
<style lang="less" scoped>
|
|
232
|
+
@import "styles/css/index.less";
|
|
233
|
+
</style>
|