使用若依上传文件

<div class="text-center">
                    <p><img class=" img-lg" id="avtarImg"></p>
                    <a href="javascript:void(0)" class="upload-img"> <label for="header">选择头像</label> </a>
                    <input type="file" class="" style="display:none" name="header" id="header" accept="image/*"/>
                </div>

js代码

function submitHandler() {
            if ($.validate.form()) {
                var formData = new FormData();
                //其他字段内容
                formData.append('name',$("#name").val());
                formData.append('mode',$("#mode").val());
                formData.append('title',$("#title").val());
                formData.append('remark',$("#remark").val());
              	//图片文件内容
                formData.append('file', $('#header')[0].files[0]);
                $.ajax({
                    url: prefix + "/add",
                    type: 'post',
                    cache: false,
                    data: formData,
                    processData: false,
                    contentType: false,
                    dataType: "json",
                    success: function(result) {
                        $.operate.successCallback(result);
                    }
                });
            }

controller代码

	/**
     * 新增信息
     */
    @RequiresPermissions("member:leader:add")
    @Log(title = "信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(MultipartFile file,Leader leader) throws IOException {
        if (file != null) {
            // 上传文件路径
            String filePath = RuoYiConfig.getUploadPath();
            // 上传并返回新文件名称
            String fileName = FileUploadUtils.upload(filePath, file);
            leader.setHeader(fileName);
        }
        return toAjax(leaderService.insertLeader(leader));
    }

你可能感兴趣的:(代码吧)