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

相关推荐
码银14 天前
在若依框架中,使用easyExcel完成动态列导出
java·excel·ruoyi
AI产品实战14 天前
95coder一句话生成MOM系统,AI用时6分50秒,Token只消耗25107
vue.js·spring boot·ai编程·ruoyi
gdhz-xwx18 天前
RuoYi-Vue-Plus,前端VXE TABLE V4.13.7
ruoyi
RuoyiOffice1 个月前
2026 年开源 BPM/工作流引擎大盘点:Flowable vs Camunda vs Activiti vs Turbo——谁才是企业级首选?
java·spring boot·后端·开源·流程图·ruoyi·anti-design-vue
RuoyiOffice1 个月前
SpringBoot+Vue3 实现 OA 公文外来文与归档台账:外部收文、BPM办理、三类公文统一归档
spring boot·微服务·uni-app·vue·ruoyi·anti-design-vue·ruoyioffice
Nick_zcy1 个月前
小说在线阅读网站和小说管理系统 · 功能全解析
java·后端·python·springboot·ruoyi
天籁晴空1 个月前
Docker Compose 部署完整指南 -- RuoYi-Vue
docker·ruoyi
向葭奔赴♡2 个月前
若依AES加密
ruoyi
程序员小崔日记2 个月前
技术之外,皆是人间
后端·ruoyi·计算机温情
伍一512 个月前
RuoYi + H2 数据库:轻量部署实战踩坑全记录
h2·ruoyi·若依