Commit 50726f98 authored by 张冬's avatar 张冬

文件上传问题全部解决

parent 01f0edcc
...@@ -4,10 +4,7 @@ ...@@ -4,10 +4,7 @@
:on-remove="handleRemove" :on-remove="handleRemove"
:on-success="handleSuccess" :on-success="handleSuccess"
:before-remove="beforeRemove" :before-remove="beforeRemove"
:limit="limit" :show-file-list="false"
:on-exceed="handleExceed"
:file-list="fileList"
:list-type="listType"
:accept="accept" :accept="accept"
:before-upload="beforeUpload" :before-upload="beforeUpload"
class="upload-demo" class="upload-demo"
...@@ -20,6 +17,12 @@ ...@@ -20,6 +17,12 @@
{{ tip }} {{ tip }}
</div> </div>
</el-upload> </el-upload>
<ul>
<li class="flex link" v-for="(item, index) in fileList" :key="index">
<div style="margin-right: 20px">{{ item.name }}</div>
<div @click="handleDelete(index, item)">删除</div>
</li>
</ul>
</div> </div>
</template> </template>
...@@ -90,19 +93,22 @@ export default { ...@@ -90,19 +93,22 @@ export default {
this.$emit('input', value); this.$emit('input', value);
}, },
beforeUpload(file) { beforeUpload(file) {
const max = this.max;
const isLt2M = file.size / 1024 / 1024 < max;
if (!isLt2M) {
this.$message.error(`上传附件大小不能超过 ${max} MB!`);
return
}
if (this.fileList.length == this.limit) {
this.$message.warning(`每次只能上传 ${this.limit} 个文件`);
return;
}
// 生成的文件名称 // 生成的文件名称
let objName = getFileNameUUID(); let objName = getFileNameUUID();
// 调用 ali-oss 中的方法 // 调用 ali-oss 中的方法
put(`${objName}${file.name}`, file).then((res) => { put(`${objName}${file.name}`, file).then((res) => {
console.log(file); console.log(file);
this.fileList.push(res); this.fileList.push(res);
const max = this.max;
const isLt2M = file.size / 1024 / 1024 < max;
if (!isLt2M) {
this.$message.error(`上传附件大小不能超过 ${max} MB!`);
}
return isLt2M;
this.emitInput(this.fileList); this.emitInput(this.fileList);
}); });
}, },
...@@ -114,9 +120,6 @@ export default { ...@@ -114,9 +120,6 @@ export default {
// handlePreview(file) { // handlePreview(file) {
// this.$emit('on-preview', file); // this.$emit('on-preview', file);
// }, // },
handleExceed(files, fileList) {
this.$message.warning(`每次只能上传 ${this.limit} 个文件`);
},
beforeRemove(file, fileList) { beforeRemove(file, fileList) {
return this.$confirm(`确定移除 ${file.name}?`); return this.$confirm(`确定移除 ${file.name}?`);
}, },
...@@ -126,6 +129,18 @@ export default { ...@@ -126,6 +129,18 @@ export default {
/** /**
* 自定义上传方法 * 自定义上传方法
*/ */
handleDelete(index, item) {
this.$confirm(`确定要删除${item.name}么?`, '提示', {
type: 'warning'
})
.then(() => {
this.$message.success('删除成功');
this.fileList.splice(index, 1);
this.$emit('input', this.fileList);
})
.catch(() => {});
},
handleUpload(option) { handleUpload(option) {
console.log(option.file); console.log(option.file);
} }
......
...@@ -141,12 +141,17 @@ export default { ...@@ -141,12 +141,17 @@ export default {
this.$emit('input', value); this.$emit('input', value);
}, },
beforeUpload(file) { beforeUpload(file) {
// 生成的文件名称 const max = this.max;
console.log(this.fileList.length, this.limit); const isLt2M = file.size / 1024 / 1024 < max;
if (!isLt2M) {
this.$message.error(`上传附件大小不能超过 ${max} MB!`);
return
}
if (this.fileList.length == this.limit) { if (this.fileList.length == this.limit) {
this.$message.warning(`每次只能上传 ${this.limit} 个文件`); this.$message.warning(`每次只能上传 ${this.limit} 个文件`);
return; return;
} }
// 生成的文件名称
let objName = getFileNameUUID(); let objName = getFileNameUUID();
// 调用 ali-oss 中的方法 // 调用 ali-oss 中的方法
put(`${objName}${file.name}`, file).then((res) => { put(`${objName}${file.name}`, file).then((res) => {
......
...@@ -169,7 +169,7 @@ ...@@ -169,7 +169,7 @@
</el-input> </el-input>
</el-form-item> </el-form-item>
<el-form-item label="附件:" prop="file"> <el-form-item label="附件:" prop="file">
<upload :tip="tip" :limit="20" :max="10" accept=".PDF,.JPG,.JPEG,.ZIP,.RAR,.docx,.doc,.docx" @input="getfile"></upload> <upload :tip="tip" :limit="20" :max="15" accept=".PDF,.JPG,.JPEG,.ZIP,.RAR,.docx,.doc,.docx" @input="getfile"></upload>
</el-form-item> </el-form-item>
</el-form> </el-form>
</div> </div>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment