4.若依框架上传文件

1.服务端代码-控制器

java 复制代码
  /**
     *  上传文件
     */
    @PostMapping("/upload")
    @Operation(summary = "上传")
    public AjaxResult uploadFile(MultipartFile file) throws Exception {
        try {
            // 上传文件路径
            String filePath = RuoYiConfig.getUploadPath();
            // 上传并返回新文件名称
            String fileName = FileUploadUtils.upload(filePath, file);
            String url = serverConfig.getUrl() + fileName;
            AjaxResult ajax = AjaxResult.success();
            ajax.put("url", url);
            ajax.put("fileName", fileName);
            ajax.put("newFileName", FileUtils.getName(fileName));
            ajax.put("originalFilename", file.getOriginalFilename());
            return ajax;
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

2.服务端代码-上传文件的方法

java 复制代码
  /**
     * 文件上传
     *
     * @param baseDir 相对应用的基目录
     * @param file 上传的文件
     * @param allowedExtension 上传文件类型
     * @return 返回上传成功的文件名
     * @throws FileSizeLimitExceededException 如果超出最大大小
     * @throws FileNameLengthLimitExceededException 文件名太长
     * @throws IOException 比如读写文件出错时
     * @throws InvalidExtensionException 文件校验异常
     */
    public static final String upload(String baseDir, MultipartFile file, String[] allowedExtension)
            throws FileSizeLimitExceededException, IOException, FileNameLengthLimitExceededException,
            InvalidExtensionException
    {
        int fileNamelength = Objects.requireNonNull(file.getOriginalFilename()).length();
        if (fileNamelength > FileUploadUtils.DEFAULT_FILE_NAME_LENGTH)
        {
            throw new FileNameLengthLimitExceededException(FileUploadUtils.DEFAULT_FILE_NAME_LENGTH);
        }

        assertAllowed(file, allowedExtension);

        String fileName = extractFilename(file);

        String absPath = getAbsoluteFile(baseDir, fileName).getAbsolutePath();
        file.transferTo(Paths.get(absPath));
        return getPathFileName(baseDir, fileName);
    }

3.前端 -JS

javascript 复制代码
// 公共方法-上传文件
export function uploadFile(file) {
  const formData = new FormData()
  formData.append('file', file)
     request({
      url: '/common/upload',
      method: 'post',
      data: formData
    }).then(res=>{

      alert(JSON.stringify(res));
    });
  }

4.前端-Vue

html 复制代码
  <el-table-column prop="date" label="文件模板" width="180">
       <template slot-scope="scope">
            <input type="file" @change="fileUploadChange($event)" />
       </template>
  </el-table-column>
javascript 复制代码
 //文件上传控件改变
    fileUploadChange(e) {
      console.log(e);
      alert(e.target.files[0]);
      uploadFile(e.target.files[0]).then((res) => {
        alert(JSON.stringify(res));
      });
    },

5. 注意点

前端传值的参数要写成data:xx;不能写成file:xx

相关推荐
Olrookie5 天前
若依前后端分离版学习笔记(二十)——实现滑块验证码(vue3)
java·前端·笔记·后端·学习·vue·ruoyi
残花月伴5 天前
如何使用若依解决多选字段的问题——方案一
ruoyi
残花月伴6 天前
若依字典原理---后端
ruoyi
笨蛋不要掉眼泪1 个月前
SpringBoot项目Excel模板下载功能详解
java·spring boot·后端·spring·excel·ruoyi
潇I洒1 个月前
若依4.8.1打包war后在Tomcat无法运行,404报错的一个解决方法
java·tomcat·ruoyi·若依·404
焯7592 个月前
若依微服务遇到的配置问题
java·mybatis·ruoyi
LKAI.2 个月前
传统方式部署(RuoYi-Cloud)微服务
java·linux·前端·后端·微服务·node.js·ruoyi
pengzhuofan3 个月前
项目一系列-第4章 在线接口文档 & 代码模板改造
低代码·ruoyi
Olrookie3 个月前
若依前后端分离版学习笔记(七)—— Mybatis,分页,数据源的配置及使用
数据库·笔记·学习·mybatis·ruoyi
Olrookie3 个月前
若依前后端分离版学习笔记(五)——Spring Boot简介与Spring Security
笔记·后端·学习·spring·ruoyi