POST http://localhost:8081/product/uploadImage 400 () dispatchXhrRequest @ xhr.js?b50d:178 xhrAdapte

vue+spring boot - 注册界面关于头像上传问题

  • 如果用饿了么的框架中的el-form 在form表单中加入上传图片的功能 即使用el-upload 如果不想在他的方法中进行上传,而是在点击注册时进行上传 他的上传办法
  • <el-upload class="avatar-uploader" action="http://localhost:8081/product/upload" :show-file-list="false" :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload"> <img v-if="ruleForm.imageUrl" :src="ruleForm.imageUrl" class="avatar"> <i v-else class="el-icon-plus avatar-uploader-icon"></i> </el-upload>

action中调用的就是上传的方法,去调用你的后台
如果这时候不想去直接完成上传的动作 则可以在后台中不完成上传操作 然后之后得到图片本身的东西

 handleAvatarSuccess(res, file) {
                this.ruleForm.imageUrl = URL.createObjectURL(file.raw);
                this.areyousure = file;
                console.log(this.areyousure)
            },

这个方法就是在完成够短之后调用 可以将你的图片得到,不过这时候得到的不是本体,如果想上传的话

let files = this.areyousure
                let file=new FormData();
                //file.push("file",files[0])
                file.append("file",files.raw)
                axios.post('http://localhost:8081/product/uploadImage',file).then(({data})=>{
                    this.ruleForm = data;

则需要在点击创建的时候调用方法 必要的一步是files.raw 必须写 这才是得到文件的本体 然后后台

@RequestMapping("/uploadImage")
    @ResponseBody
    public String uploadImage(@RequestParam MultipartFile file){
        String sqlPath = null;
        String localPath="F:\\vuezuo\\vuehelloworld\\public\\static\\picture\\";
        String filename=null;
        //System.out.println(file);
        if(!file.isEmpty()){
            //生成uuid作为文件名称
            String uuid = UUID.randomUUID().toString().replaceAll("-","");
            //获得文件类型(可以判断如果不是图片,禁止上传)
            String contentType=file.getContentType();
            //获得文件后缀名
            String suffixName=contentType.substring(contentType.indexOf("/")+1);
            //得到 文件名
            filename=uuid+"."+suffixName;
            //文件保存路径
            try {
                file.transferTo(new File(localPath+filename));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        sqlPath = localPath+filename;
        System.err.println(sqlPath);
        //System.out.println(sqlPath);
        // System.out.println(userService.findTouxiang(sqlPath).toString());
        return sqlPath;
    }

这个错误主要原因就是files.raw没有写 就会报错

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值