vue-3d-mode
npm install vue-3d-model
ObjUpload 组件 上传组件
<template>
<div class="upload-file">
<div class="upload-obj-file">
<el-upload
multiple
:action="uploadFileUrl"
:before-upload="handleBeforeUpload"
:file-list="fileList"
:limit="limit"
:on-error="handleUploadError"
:on-exceed="handleExceed"
:on-success="handleUploadSuccess"
:show-file-list="false"
:headers="headers"
class="upload-file-uploader"
ref="upload"
>
<el-button size="mini" type="primary">选取文件</el-button>
</el-upload>
<el-button type="danger" size="mini" class="upload-file-uploader el-upload el-upload--text delete_obj" @click="handleDelete">删除文件</el-button>
</div>
<div class="el-upload__tip" slot="tip" v-if="showTip">
请上传
<template v-if="fileSize"> 大小不超过 <b style="color: #f56c6c">{{ fileSize }}MB</b> </template>
<template v-if="fileType"> 格式为 <b style="color: #f56c6c">{{ fileType.join("/") }}</b> </template>
的文件
</div>
<div class="objFile">
<model-obj :src="`${baseUrl}${objUrl}`" :width="100" :height="100" class="objFile"></model-obj>
</div>
</div>
</template>
<script>
import { getToken } from "@/utils/auth";
import { ModelObj } from "vue-3d-model";
import {deleteObjFile} from "@/api/system/common"
export default {
name: "objUpload",
components:{
ModelObj
},
props: {
value: [String, Object, Array],
limit: {
type: Number,
default: 5,
},
fileSize: {
type: Number,
default: 200,
},
fileType: {
type: Array,
default: () => ["obj"],
},
isShowTip: {
type: Boolean,
default: true
}
},
data() {
return {
objUrl:'',
number: 0,
uploadList: [],
multiple: true,
baseUrl: process.env.VUE_APP_BASE_API,
uploadFileUrl: process.env.VUE_APP_BASE_API + "/common/objUpload",
headers: {
Authorization: "Bearer " + getToken(),
},
fileList: [],
};
},
watch: {
value: {
handler(val) {
this.objUrl = val
},
deep: true,
immediate: true
}
},
computed: {
showTip() {
return this.isShowTip && (this.fileType || this.fileSize);
},
},
methods: {
handleBeforeUpload(file) {
if (this.fileType) {
let fileExtension = "";
if (file.name.lastIndexOf(".") > -1) {
fileExtension = file.name.slice(file.name.lastIndexOf(".") + 1);
}
const isTypeOk = this.fileType.some((type) => {
if (file.type.indexOf(type) > -1) return true;
if (fileExtension && fileExtension.indexOf(type) > -1) return true;
return false;
});
if (!isTypeOk) {
this.$modal.msgError(`文件格式不正确, 请上传${this.fileType.join("/")}格式文件!`);
return false;
}
}
if (this.fileSize) {
const isLt = file.size / 1024 / 1024 < this.fileSize;
if (!isLt) {
this.$modal.msgError(`上传文件大小不能超过 ${this.fileSize} MB!`);
return false;
}
}
this.$modal.loading("正在上传文件,请稍候...");
this.number++;
return true;
},
handleExceed() {
this.$modal.msgError(`上传文件数量不能超过 ${this.limit} 个!`);
},
handleUploadError(err) {
this.$modal.msgError("上传文件失败,请重试");
this.$modal.closeLoading()
},
handleUploadSuccess(res) {
const objUrl = res.objUrl;
this.objUrl = objUrl
this.$emit("input",objUrl)
this.$modal.closeLoading();
},
handleDelete() {
deleteObjFile(this.objUrl).then(response=>{
this.$modal.msgSuccess("删除成功");
this.objUrl= "";
})
},
getFileName(name) {
if (name.lastIndexOf("/") > -1) {
return name.slice(name.lastIndexOf("/") + 1);
} else {
return "";
}
},
listToString(list, separator) {
let strs = "";
separator = separator || ",";
for (let i in list) {
strs += list[i].url + separator;
}
return strs != '' ? strs.substr(0, strs.length - 1) : '';
}
}
};
</script>
<style scoped lang="scss">
.objFile{
border: 1rpx solid #ccc;
height: 100px;
width: 100rpx;
}
.upload-file-uploader {
margin-bottom: 5px;
}
.upload-file-list .el-upload-list__item {
border: 1px solid #e4e7ed;
line-height: 2;
margin-bottom: 10px;
position: relative;
}
.upload-file-list .ele-upload-list__item-content {
display: flex;
justify-content: space-between;
align-items: center;
color: inherit;
}
.ele-upload-list__item-content-action .el-link {
margin-right: 10px;
}
.upload-obj-file{
display: flex;
align-items: center;
}
.delete_obj{
margin-left: 10px;
}
</style>
Main.js
//Obj 图片上传组件且回显
import ObjUpload from "@/components/ObjUpload"
Vue.component("ObjUpload",ObjUpload)
notice 文件下 index.vue (通知公告)
<el-col :span="24">
<el-form-item label="内容">
<editor v-model="form.noticeContent" :min-height="192"/>
</el-form-item>
</el-col>
<el-col :span="12">
<el-form-item label="上传组件">
<objUpload v-model="form.pdfUrl"></objUpload>
</el-form-item>
</el-col>
// 表单重置 重置按钮操作
reset() {
this.form = {
noticeId: undefined,
noticeTitle: undefined,
noticeType: undefined,
noticeContent: undefined,
status: "0",
pdfUrl: undefined, // 存储obj 文件的路径
};
this.resetForm("form");
},
新建common.js 文件 写删除文件接口
import request from '@/utils/request'
//
export function deleteObjFile(query) {
return request({
url: '/common/deleteObjFile',
method: 'delete',
data: query
})
}
java 上传obj 文件 和 删除obj 接口
@PostMapping("/objUpload")
public AjaxResult objUploadFile(MultipartFile file) throws Exception
{
try
{
String obj = FileUploadUtils.upload(RuoYiConfig.getObjFilePath(), file, MimeTypeUtils.Obj_EXTENSION);
AjaxResult ajaxResult = AjaxResult.success();
ajaxResult.put("objUrl", obj);
return ajaxResult;
} catch (Exception e)
{
return AjaxResult.error(e.getMessage());
}
}
@DeleteMapping("/deleteObjFile")
public AjaxResult deleteObjFile(@RequestBody String query)
{
try
{
int firstSlashIndex = query.indexOf('/');
int secondSlashIndex = query.indexOf('/', firstSlashIndex + 1);
FileUtils.deleteFile(objFilePath + query.substring(secondSlashIndex));
return AjaxResult.success();
} catch (Exception e)
{
return AjaxResult.error(e.getMessage());
}
}
}
效果展示
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/d6056597f7a888c862488bfb20cc60c3.png)