美文网首页axios
Vue axios请求params&data 详解-formDa

Vue axios请求params&data 详解-formDa

作者: 李鼎JohnLi | 来源:发表于2020-02-26 21:25 被阅读0次

    formData的坑坑坑

    uploadFiles(file){
                    // debuggersaveCollectWithFile
                    let formData = new FormData();  //此处有坑,附件上传必须用formData
                    // formData.set("file", file.file);  //后端是根据"file" 来进行取值
                    formData.append("file", file.file);  //后端是根据"file" 来进行取值
                    this.saveCollectWithFileCons.collectNameListId ="aaa";
                    this.saveCollectWithFileCons.collectNameListName="bbb";
                    formData.append("saveCollectWithFileCons",this.saveCollectWithFileCons);
                    debugger
                    uploadFiles(formData).then(res => {
                        this.status = res.status
                        this.message = res.message
                        console.log('this.status:  ' + this.status)
                        // console.log('refs.length2:  ' + that.$refs.upload.$children[0].fileList.length)
                        // debugger
                        if (this.status == 0) {
                            // debugger
                            this.$message.success(`附件上传成功`)
                        } else {
                            this.$message.info(`附件上传失败,失败原因${this.message}`)
                        }
                    }).catch(err => {
                        // debugger
                        console.log('uploadFiles-err:  ' + err)
                        if(err.toString().indexOf('cancel') == -1){
                            this.$message.error(`删除失败,失败原因为:${err}`)
                            return false;
                        }
                    });
                },
    
    

    axios index.js文件

    //附件上传
    export const uploadFiles = query => {
        return request({
            // url: '/list-lib/uploadFileResult.json',
            // method: 'get',
            url: '/fileList/uploadFiles',
            method: 'post',
            data:query   // 附件上传只能用data 来传数据
        }).catch(error => {
            // console.log('error:---   ' + JSON.stringify(error));
            // alert(error)
        });
    };
    
    

    后台代码:

        @ApiOperation(value = "上传附件",notes = "注意事项....",httpMethod = "POST")
    //    @ApiImplicitParam(dataType = "Long",name = "name123",value="value123",required = true)
        @RequestMapping(value = "uploadFiles",method = RequestMethod.POST)
    //    @PostMapping("uploadFiles")
    //    附件&多参数传递,目前没解决---------------------
    //    RequestParam 用来取axios 中param 的值,RequestBody 用来去axios中data 的值
    //    FormData 传递过来的值,可以用RequestParam & RequestBody 来接收
    //    public Map uploadFiles(@RequestParam("file") MultipartFile file,@RequestParam("saveCollectWithFileCons") String saveCollectWithFileCons) {
        public Map uploadFiles(@RequestBody MultipartFile file, String saveCollectWithFileCons) {
            System.out.println(saveCollectWithFileCons);
            Map resultMap = new HashMap();
            String status="0";
            String message="附件上传成功";
            resultMap.put("status",status);
            resultMap.put("message",message);
            if (file.isEmpty()) {
                status = "1";
                message="上传文件不能为空";
                resultMap.put("status",status);
                resultMap.put("message",message);
                return resultMap;
            }else{
            //上传文件 相关逻辑
                String fileName = file.getOriginalFilename();
                File dest = new File(uploadPath+fileName);
                try {
                    file.transferTo(dest);
                    return resultMap;
                } catch (IOException e) {
                    status="2";
                    message="附件上传失败.";
                    resultMap.put("status",status);
                    resultMap.put("message",message);
                    e.printStackTrace();
                }
    
                return resultMap;
            }
        }
    
    

    // RequestParam 用来取axios 中param 的值,RequestBody 用来去axios中data 的值
    // FormData 传递过来的值,可以用RequestParam & RequestBody 来接收

    相关文章

      网友评论

        本文标题:Vue axios请求params&data 详解-formDa

        本文链接:https://www.haomeiwen.com/subject/yvtgchtx.html