vue图片上传

前端代码:

 <el-upload
                                            style="height: 200px;"
                                            class="upload-demo"
                                            ref="uploadVideoCoverDemo" 
                                            :action="http://localhost:8081/imageupload/common/imguploadurl.mvc"
                                            :before-upload="videoCoverBefore"
                                            :on-success="succVideoCoverUpload"
                                            :auto-upload="false"  
                                            accept="image/*"

                                            :multiple=“false”
                                            :limit="1"
                                            :file-list="fileList4"
                                    >
                                        <el-button slot="trigger" size="small" type="primary">选择视频封面图</el-button>
                                        <el-button style="margin-left: 10px;" size="small" type="success" @click="submitVideoCover">上 传</el-button>

                                    </el-upload>

释义:

ref="uploadVideoCoverDemo"  :手动上传时作为上传时的引用

:action="http://localhost:8081/imageupload/common/imguploadurl.mvc":上传文件路径

 :before-upload="videoCoverBefore":请求路径

:before-upload="videoCoverBefore":上传前要执行的钩子函数,可对文件进行各项操作 例如大小之类的判断

:on-success="succVideoCoverUpload":上传成功之后执行的钩子函数

:auto-upload="false"  :是否自动上传  true为自动上传 false为手动上传,当为true时:ref将不起作用,选择则好文件之后就会立马上传;当为false时:选择好文件之后还需点击上传按钮调用submitVideoCover之后才能上传,此时需要使用到ref的值作为引用

accept="image/*":上传文件类型    audio/*代表声音文件  video/*代表视频文件  image/*代表图像文件    //可自行修改上传文件类型,上传方式均相同

:multiple=“false” :是否允许多文件上传   为true时:则可以进行多文件上传 但是可以用limit进行上传个数限制    注意:多文件上传原理实际上也是单个文件上传,不过与单文件上传的区别在于,单文件上传是只能选择一个进行上传,多文件上传是选择多个文件之后,自动一个一个发送请求进行上传,如果有返回参数的话,则参数是一个一个返回。

export default{

 name: 'article-editor',

data(){

    return{

          fileList4:[]        //初始化参数  为空数组

    },methods:{

              submitVideoCover(){ this.$refs.uploadVideoCoverDemo.submit()},  //手动文件上传 

               videoCoverBefore(file){//文件上传前执行的钩子函数    

                   var fileName = new Array();

                    fileName = file.name.split('.');

                    const isLt150KB = file.size / 1024 / 1024 < 0.15; //判断图片大小
                    const extension = fileName[fileName.length-1] === 'jpg'; //判断图片格式
                    const extension2 =  fileName[fileName.length-1]=== 'png';
                    if (!extension && !extension2) {  
                       this.$message({
                          message: '上传模板只能是jpg、png格式!',
                          type: 'warning'
                     });
                       return false;
                  }
                  if (!isLt150KB) {
                       this.$message({
                           message: '上传模板大小不能大于 150k!',
                          type: 'warning'
                      });
                      return false;
                  }

                },

         /**
             * 上传成功之后的回调函数
             *
             */
            succVideoCoverUpload(response, file, fileList3) {
                let _this = this;
                if(response.bool === 'false'){
                    _this.fileList4 = [];//上传失败时,清除数据,再次重新选择
                    _this.$message({
                        message: '上传失败!',
                        type: 'warning'
                    });
                    return;
                }
                if(response.bool == 'true'){
                    $.ajax({
                        type:'post',
                        url:url,//路径  可在路径上面携带参数
                        dataType: 'json',
                        cache: false,//上传文件无需缓存
                        processData: false,//用于对data参数进行序列化处理 这里必须false
                        contentType: false, //必须
                        success:function(data){
                            console.log(data);//输出返回的数据
                        },error:function(data){
                            console.log(data);
                        }
                    })
                }
            }

       }

  }

}

 

后端Java代码:


@Controller
@RequestMapping("/imageupload/common")
public class ImageUpload extends CommonUtils{

    /**
     * 图片上传
     * @return
     */
    @RequestMapping(value="/imguploadurl",method = RequestMethod.POST)
    @ResponseBody
 public Map<String,String> importEnterPriseQuota(Integer userId,@RequestParam("file") MultipartFile proFile, HttpServletRequest request) {
        String path = request.getSession().getServletContext().getRealPath("/upload/active/");
        String filename = proFile.getOriginalFilename();
        File filepath = new File(path,filename);
        Map<String,String> resMap = new HashMap<String,String>();
        try {
            if (!filepath.getParentFile().exists()) {
                filepath.getParentFile().mkdirs();
            }
            proFile.transferTo(new File(path+File.separator+filename));
            resMap.put("bool", "true");
            resMap.put("path", "/upload/active/"+filename);
            resMap.put("realPath",path+File.separator+filename);
        } catch (IOException e) {
            e.printStackTrace();
            resMap.put("bool", "false");
        }
        return resMap;
    }
}
 

 

 

 

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值