Vue上传文件+普通参数

 /*
        Submits files to the server
      */
      submitFiles(){
        /*
          Initialize the form data
        */
        let formData = new FormData();
        /*
          Iteate over any file sent over appending the files
          to the form data.
        */
        for( var i = 0; i < this.files.length; i++ ){
          let file = this.files[i];
          formData.append('picture', file);
          formData.append('name',"测试课程");
           formData.append('introduction',"这说vd");
        }
        /*
          Make the request to the POST /select-files URL
        */
         this.$axios.post( '/teacher/createCourse',  formData,{ headers: {
                'Content-Type': 'multipart/form-data'
            }},
   
        
         
        ).then(function(){
          console.log('SUCCESS!!');
        })
        .catch(function(){
          console.log('FAILURE!!');
        });
      },

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Vue.js中使用ElementUI实现文件上传功能,可以按照以下步骤进行操作: 1. 安装ElementUI和axios: ``` npm install element-ui axios ``` 2. 在Vue项目中引入ElementUI和axios: ```javascript import Vue from 'vue' import ElementUI from 'element-ui' import 'element-ui/lib/theme-chalk/index.css' import axios from 'axios' Vue.use(ElementUI) Vue.prototype.$axios = axios ``` 3. 在组件中添加文件上传组件: ```html <template> <div> <el-upload class="upload-demo" action="/upload" :on-success="handleSuccess" :before-upload="beforeUpload" :file-list="fileList" :auto-upload="false"> <el-button size="small" type="primary">点击上传</el-button> <div slot="tip" class="el-upload__tip">只能上传jpg/png文件,且不超过500kb</div> </el-upload> </div> </template> ``` 在`el-upload`组件中,我们指定了上传文件的地址和一些属性,例如`before-upload`和`on-success`。在`before-upload`中,我们可以对上传的文件进行一些校验,例如文件类型和大小;在`on-success`中,我们可以处理上传成功后的回调。 4. 在组件的`data`选项中添加文件列表: ```javascript export default { data() { return { fileList: [] } }, methods: { handleSuccess(response, file, fileList) { // 处理上传成功后的回调 }, beforeUpload(file) { // 对上传的文件进行校验 } } } ``` 5. 在`beforeUpload`方法中,我们可以通过`return false`来阻止上传,或者通过`return true`来允许上传。在这个方法中,我们还可以使用ElementUI的`Message`组件来提示用户上传文件的限制条件: ```javascript beforeUpload(file) { const isJPG = file.type === 'image/jpeg' || file.type === 'image/png' const isLt2M = file.size / 1024 / 1024 < 2 if (!isJPG) { this.$message.error('上传图片只能是 JPG/PNG 格式!') } if (!isLt2M) { this.$message.error('上传图片大小不能超过 2MB!') } return isJPG && isLt2M } ``` 这样,我们就可以在Vue.js中使用ElementUI实现文件上传功能了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值