vue + elmentPlus upload文件上传列表回显

<el-dialog
      v-model="passVisible"
      :title="rejectVisible ? '填写通过原因' : '填写拒绝原因'"
      width="50%"
      :before-close="handleClose"
      align-center
    >
      <el-input v-model="resons" :rows="2" type="textarea" placeholder="请输入原因" />
      <el-upload
        ref="uploadRef"
        :auto-upload="true"
        v-model:file-list="fileList"
        class="upload-demo mt-20px"
        :http-request="uploadExperienceList"
        :before-upload="beforerUpload"
        :on-preview="handlePreview"
        :on-remove="handleRemove"
        :before-remove="beforeRemove"//与before-upload同时使用需设置beforeRemove返回ture
        :on-exceed="handleExceed"
        :limit="3"
        multiple
        
      >
        <el-button type="primary">上传附件</el-button>
        <!-- <template #tip>
          <div class="el-upload__tip"> jpg/png files with a size less than 500KB. </div>
        </template> -->
      </el-upload>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="passVisible = false">取消</el-button>
          <el-button type="success" @click="saveReson"> 确认 </el-button>
        </span>
      </template>
 </el-dialog>

// 上传可用钩子函数
import { ElMessage } from 'element-plus'
import type { UploadProps, UploadUserFile } from 'element-plus'

const fileList = ref<UploadUserFile[]>([])
const handleRemove: UploadProps['onRemove'] = (file, uploadFiles) => {
  console.log(file, uploadFiles)
}
const handlePreview: UploadProps['onPreview'] = (uploadFile) => {
  console.log(uploadFile)
}
const handleExceed: UploadProps['onExceed'] = (files, uploadFiles) => {
  ElMessage.warning(
    `The limit is 3, you selected ${files.length} files this time, add up to ${
      files.length + uploadFiles.length
    } totally`
  )
}
const beforeRemove: UploadProps['beforeRemove'] = (uploadFile, uploadFiles) => {
  return ElMessageBox.confirm(`确认删除 ${uploadFile.name} ?`).then(
    () => true,
    () => false
  )
}
// 上传文件类限制
const beforerUpload = (file) => {
  let FileExt = file.name.replace(/.+\./, '')
  if (['pdf', 'doc', 'docx', 'xls', 'xlsx'].indexOf(FileExt.toLowerCase()) === -1) {
    ElMessage({
      message: '请上传.pdf、.doc、.docx、.xls、.xlsx格式的文件',
      type: 'error'
    })
    return false
  } else if (file.size / 1024 / 1024 > 50) {
    ElMessage.error('文件大小不能超过50MB!')
    return false
  }
}
//上传后过滤文件fileList列表
let fileIds: any = ref([])
const uploadExperienceList: any = (param: UploadRequestOptions) => {
  let formData = new FormData()
  formData.append('file', param.file)
  common.uploadFileApi(formData).then((res) => {
    fileList.value.push({ name: param.file.name, ...res[0] })
    fileList.value = fileList.value.filter((item: any) => item.id)
    ElMessage.success('上传成功')
  })
}
//提交后过滤文件列表,列表回显通过控制fileList实现
const saveReson = async () => {
  fileIds.value = fileList.value.map((item: any) => item.id)
    let params = {
      id: idPass.value,
      fileIds: fileIds.value,
      comment: resons.value
    }
    if (rejectVisible.value) {
      apply.pass(params).then(() => {
        ElMessage.success('操作成功')
        getResultData()
        passVisible.value = false
      })
    } else {
      apply.refuse(params).then(() => {
        ElMessage.success('操作成功')
        getResultData()
        passVisible.value = false
      })
    }
}

// 上传文件接口定义
export const uploadFileApi = async (data) => {
  return request.post({
    url: '/api/uploads/files',
    data,
    headersType: 'application/x-www-form-urlencoded multipart/form-data'
  })
}

  • 14
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
如果你想要实现从 MySQL 数据库中获取文件和表单数据,并在前端页面中显示出来,那么你可以按照以下步骤进行操作: 1. 创建 MySQL 数据库表格,并将文件和表单数据存储在其中。 2. 使用 Spring Boot 创建 RESTful API 接口,从 MySQL 数据库中获取文件和表单数据。 3. 在 Vue.js 前端页面中使用 axios 库调用 Spring Boot 的 RESTful API 接口,获取数据,并将数据展示在页面上。 4. 使用 Element UI 框架中的组件来展示表单数据和文件数据。 下面是具体实现步骤: 1. 创建 MySQL 数据库表格 首先,你需要创建一个 MySQL 数据库表格,来存储文件和表单数据。可以参考以下示例表格: ``` CREATE TABLE `file` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `name` varchar(255) NOT NULL, `type` varchar(255) NOT NULL, `size` bigint(20) NOT NULL, `content` longblob NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci; CREATE TABLE `form_data` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `name` varchar(255) NOT NULL, `age` int(11) NOT NULL, `gender` varchar(10) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci; ``` 2. 使用 Spring Boot 创建 RESTful API 接口 在 Spring Boot 中,你可以使用 JPA 来访问数据库,获取文件和表单数据。以下是示例代码: ``` @RestController @RequestMapping("/api") public class FileController { @Autowired private FileRepository fileRepository; @Autowired private FormDataRepository formDataRepository; @PostMapping("/file") public ResponseEntity<?> uploadFile(@RequestParam("file") MultipartFile file) throws IOException { String fileName = StringUtils.cleanPath(file.getOriginalFilename()); FileModel fileModel = new FileModel(fileName, file.getContentType(), file.getSize(), file.getBytes()); fileRepository.save(fileModel); return new ResponseEntity<>("File uploaded successfully", HttpStatus.OK); } @GetMapping("/file") public ResponseEntity<?> getAllFiles() { List<FileModel> files = fileRepository.findAll(); return new ResponseEntity<>(files, HttpStatus.OK); } @GetMapping("/form-data") public ResponseEntity<?> getAllFormData() { List<FormDataModel> formDataList = formDataRepository.findAll(); return new ResponseEntity<>(formDataList, HttpStatus.OK); } @PostMapping("/form-data") public ResponseEntity<?> createFormData(@RequestBody FormDataModel formDataModel) { formDataRepository.save(formDataModel); return new ResponseEntity<>("Form data created successfully", HttpStatus.OK); } } ``` 3. 在 Vue.js 前端页面中使用 axios 库调用 Spring Boot 的 RESTful API 接口 在 Vue.js 中,你可以使用 axios 库来发送 HTTP 请求,获取数据。以下是示例代码: ``` <template> <div> <el-table :data="formDataList" style="width: 100%"> <el-table-column prop="name" label="Name"></el-table-column> <el-table-column prop="age" label="Age"></el-table-column> <el-table-column prop="gender" label="Gender"></el-table-column> </el-table> <el-upload class="upload-demo" action="/api/file" :on-success="handleSuccess" :show-file-list="false" > <el-button slot="trigger" size="small" type="primary">Click to Upload</el-button> <div slot="tip" class="el-upload__tip">jpg/png files with a size less than 500kb</div> </el-upload> <el-table :data="fileList" style="width: 100%"> <el-table-column prop="name" label="Name"></el-table-column> <el-table-column prop="type" label="Type"></el-table-column> <el-table-column prop="size" label="Size"></el-table-column> <el-table-column label="Action"> <template slot-scope="scope"> <el-button type="text" @click="handleDownload(scope.row)">Download</el-button> </template> </el-table-column> </el-table> </div> </template> <script> import axios from 'axios' export default { data() { return { formDataList: [], fileList: [] } }, mounted() { this.getFormDataList() this.getFileList() }, methods: { getFormDataList() { axios.get('http://localhost:8080/api/form-data') .then(response => { this.formDataList = response.data }) .catch(error => console.log(error)) }, getFileList() { axios.get('http://localhost:8080/api/file') .then(response => { this.fileList = response.data }) .catch(error => console.log(error)) }, handleSuccess(response) { this.$message.success('File uploaded successfully') }, handleDownload(row) { const blob = new Blob([row.content]) const link = document.createElement('a') link.href = URL.createObjectURL(blob) link.download = row.name link.click() } } } </script> ``` 4. 使用 Element UI 框架中的组件来展示表单数据和文件数据 在 Vue.js 中,你可以使用 Element UI 框架中的组件来展示表单数据和文件数据。以上面的示例代码为例,我们使用了 el-table 和 el-upload 组件来展示表单数据和文件数据。你可以根据你的需求选择合适的组件来展示数据。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值