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

相关推荐
焯7596 天前
若依微服务遇到的配置问题
java·mybatis·ruoyi
LKAI.22 天前
传统方式部署(RuoYi-Cloud)微服务
java·linux·前端·后端·微服务·node.js·ruoyi
pengzhuofan1 个月前
项目一系列-第4章 在线接口文档 & 代码模板改造
低代码·ruoyi
Olrookie1 个月前
若依前后端分离版学习笔记(七)—— Mybatis,分页,数据源的配置及使用
数据库·笔记·学习·mybatis·ruoyi
Olrookie1 个月前
若依前后端分离版学习笔记(五)——Spring Boot简介与Spring Security
笔记·后端·学习·spring·ruoyi
草履虫建模2 个月前
前后端分离项目中的接口设计与调用流程——以高仙机器人集成为例
java·前端·spring boot·机器人·intellij-idea·ruoyi·js
弗锐土豆2 个月前
一个基于若依(ruoyi-vue3)的小项目部署记录
前端·vue.js·部署·springcloud·ruoyi·若依
night 猿3 个月前
ruoyi-vue3.9.0替换mybatis为mybatisPlus
java·ruoyi·ruoyi-vue
胡斌附体3 个月前
ruoyi-flowable-plus中satoken的配置使用
ruoyi·配置·登录·拦截器·satoken
越来越无动于衷3 个月前
若依项目AI 助手代码解析
vue.js·人工智能·elementui·ruoyi