vue+el-upload实现单文件多文件上传

该博客详细介绍了如何利用Vue.js结合Element UI库,分别实现单文件和多文件的上传功能,包括具体步骤和关键配置。
摘要由CSDN通过智能技术生成

vue+el-upload实现单文件多文件上传

1、单文件上传
<el-form-item :label="$t('Version.Attachment')">
   <el-upload
     class="upload-demo"
     action="#"
     :headers="myHeaders"
     :before-remove="beforeRemove"
     :on-change="nowFile"
     :multiple="false"
     :auto-upload="false"
   >
     <el-button size="small" type="primary" plain>选择附件</el-button>
     <div slot="tip" v-if="showTip">只能上传apk文件</div>
   </el-upload>
 </el-form-item>
 <div class="text-center">
    <el-button size="medium" type="primary" @click="checkSave()">{
  {
      $t("buttons.save")
    }}</el-button>
  </div>
var token = window.localStorage.getItem("access_token");
export default {
   
  data() {
   
    return {
   
      versionDetail: {
   },
      showTip: true, // 是否显示上传apk文件提示
      myHeaders: {
    Authorization: "bearer " + token },
      files:{
   }, // 要上传的文件
      keyId:''  // 上传文件参数
    };
  },
  created() {
   
    this.getData(1);
  },
  methods: {
   
	// 删除文件之前的钩子
    beforeRemove(file, fileList) {
   
      this.$confirm(`确定移除 ${
     file.name}`,"提示",{
   
        confirmButtonText: "确定",
        cancelButtonText: "取消",
      }).then(()=>{
   
        this.files = {
   }
        this.sh
实现Vue + Axios + El-upload进行文件上传并跨域,需要按照以下步骤进行操作: 1. 在Vue项目中安装axios和element-ui。在命令行中输入以下命令即可安装: ``` npm install axios element-ui --save ``` 2. 在Vue项目中创建一个上传文件的组件,例如FileUpload.vue。在该组件中导入axios和element-ui,并且引入El-upload组件。示例代码如下: ```vue <template> <div> <el-upload class="upload-demo" :action="uploadUrl" :on-success="handleUploadSuccess" :before-upload="beforeUpload" :headers="headers" :data="formData" :file-list="fileList"> <el-button size="small" type="primary">点击上传</el-button> </el-upload> </div> </template> <script> import axios from 'axios' import { Message } from 'element-ui' export default { name: 'FileUpload', data () { return { fileList: [], uploadUrl: 'http://example.com/upload', formData: {}, headers: { 'Content-Type': 'multipart/form-data' } } }, methods: { handleUploadSuccess (response, file, fileList) { // 上传成功后的处理逻辑 console.log(response) }, beforeUpload (file) { // 文件上传前的处理逻辑 console.log(file) } } } </script> ``` 3. 在组件中实现文件上传的逻辑。在上传文件之前,需要设置请求头和请求数据,并且需要处理跨域请求。可以在组件的methods中定义一个upload方法,用来发送上传请求。示例代码如下: ```vue <script> import axios from 'axios' import { Message } from 'element-ui' export default { name: 'FileUpload', data () { return { fileList: [], uploadUrl: 'http://example.com/upload', formData: {}, headers: { 'Content-Type': 'multipart/form-data' } } }, methods: { handleUploadSuccess (response, file, fileList) { // 上传成功后的处理逻辑 console.log(response) }, beforeUpload (file) { // 文件上传前的处理逻辑 console.log(file) }, upload () { let config = { headers: this.headers, withCredentials: true // 跨域请求需要设置withCredentials为true } let data = new FormData() data.append('file', this.fileList[0].raw) // 向后端发送上传请求 axios.post(this.uploadUrl, data, config) .then(response => { this.handleUploadSuccess(response) }) .catch(error => { console.log(error) }) } } } </script> ``` 4. 最后,在Vue组件中使用El-upload组件,并且调用upload方法即可实现文件上传。示例代码如下: ```vue <template> <div> <el-upload class="upload-demo" :action="uploadUrl" :on-success="handleUploadSuccess" :before-upload="beforeUpload" :headers="headers" :data="formData" :file-list="fileList"> <el-button size="small" type="primary" @click="upload">点击上传</el-button> </el-upload> </div> </template> <script> import axios from 'axios' import { Message } from 'element-ui' export default { name: 'FileUpload', data () { return { fileList: [], uploadUrl: 'http://example.com/upload', formData: {}, headers: { 'Content-Type': 'multipart/form-data' } } }, methods: { handleUploadSuccess (response, file, fileList) { // 上传成功后的处理逻辑 console.log(response) }, beforeUpload (file) { // 文件上传前的处理逻辑 console.log(file) }, upload () { let config = { headers: this.headers, withCredentials: true // 跨域请求需要设置withCredentials为true } let data = new FormData() data.append('file', this.fileList[0].raw) // 向后端发送上传请求 axios.post(this.uploadUrl, data, config) .then(response => { this.handleUploadSuccess(response) }) .catch(error => { console.log(error) }) } } } </script> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值