Search in sources :

Example 1 with SysFile

use of com.ruoyi.system.api.domain.SysFile in project RuoYi-Cloud by yangzongzhuan.

the class SysFileController method upload.

/**
 * 文件上传请求
 */
@PostMapping("upload")
public R<SysFile> upload(MultipartFile file) {
    try {
        // 上传并返回访问地址
        String url = sysFileService.uploadFile(file);
        SysFile sysFile = new SysFile();
        sysFile.setName(FileUtils.getName(url));
        sysFile.setUrl(url);
        return R.ok(sysFile);
    } catch (Exception e) {
        log.error("上传文件失败", e);
        return R.fail(e.getMessage());
    }
}
Also used : SysFile(com.ruoyi.system.api.domain.SysFile) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Example 2 with SysFile

use of com.ruoyi.system.api.domain.SysFile in project RuoYi-Cloud-Oracle by yangzongzhuan.

the class SysProfileController method avatar.

/**
 * 头像上传
 */
@Log(title = "用户头像", businessType = BusinessType.UPDATE)
@PostMapping("/avatar")
public AjaxResult avatar(@RequestParam("avatarfile") MultipartFile file) throws IOException {
    if (!file.isEmpty()) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        R<SysFile> fileResult = remoteFileService.upload(file);
        if (StringUtils.isNull(fileResult) || StringUtils.isNull(fileResult.getData())) {
            return AjaxResult.error("文件服务异常,请联系管理员");
        }
        String url = fileResult.getData().getUrl();
        if (userService.updateUserAvatar(loginUser.getUsername(), url)) {
            AjaxResult ajax = AjaxResult.success();
            ajax.put("imgUrl", url);
            // 更新缓存用户头像
            loginUser.getSysUser().setAvatar(url);
            tokenService.setLoginUser(loginUser);
            return ajax;
        }
    }
    return AjaxResult.error("上传图片异常,请联系管理员");
}
Also used : AjaxResult(com.ruoyi.common.core.web.domain.AjaxResult) LoginUser(com.ruoyi.system.api.model.LoginUser) SysFile(com.ruoyi.system.api.domain.SysFile) PostMapping(org.springframework.web.bind.annotation.PostMapping) Log(com.ruoyi.common.log.annotation.Log)

Example 3 with SysFile

use of com.ruoyi.system.api.domain.SysFile in project RuoYi-Cloud-Oracle by yangzongzhuan.

the class SysFileController method upload.

/**
 * 文件上传请求
 */
@PostMapping("upload")
public R<SysFile> upload(MultipartFile file) {
    try {
        // 上传并返回访问地址
        String url = sysFileService.uploadFile(file);
        SysFile sysFile = new SysFile();
        sysFile.setName(FileUtils.getName(url));
        sysFile.setUrl(url);
        return R.ok(sysFile);
    } catch (Exception e) {
        log.error("上传文件失败", e);
        return R.fail(e.getMessage());
    }
}
Also used : SysFile(com.ruoyi.system.api.domain.SysFile) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Example 4 with SysFile

use of com.ruoyi.system.api.domain.SysFile in project RuoYi-Cloud by yangzongzhuan.

the class SysProfileController method avatar.

/**
 * 头像上传
 */
@Log(title = "用户头像", businessType = BusinessType.UPDATE)
@PostMapping("/avatar")
public AjaxResult avatar(@RequestParam("avatarfile") MultipartFile file) throws IOException {
    if (!file.isEmpty()) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        R<SysFile> fileResult = remoteFileService.upload(file);
        if (StringUtils.isNull(fileResult) || StringUtils.isNull(fileResult.getData())) {
            return AjaxResult.error("文件服务异常,请联系管理员");
        }
        String url = fileResult.getData().getUrl();
        if (userService.updateUserAvatar(loginUser.getUsername(), url)) {
            AjaxResult ajax = AjaxResult.success();
            ajax.put("imgUrl", url);
            // 更新缓存用户头像
            loginUser.getSysUser().setAvatar(url);
            tokenService.setLoginUser(loginUser);
            return ajax;
        }
    }
    return AjaxResult.error("上传图片异常,请联系管理员");
}
Also used : AjaxResult(com.ruoyi.common.core.web.domain.AjaxResult) LoginUser(com.ruoyi.system.api.model.LoginUser) SysFile(com.ruoyi.system.api.domain.SysFile) PostMapping(org.springframework.web.bind.annotation.PostMapping) Log(com.ruoyi.common.log.annotation.Log)

Aggregations

SysFile (com.ruoyi.system.api.domain.SysFile)4 PostMapping (org.springframework.web.bind.annotation.PostMapping)4 AjaxResult (com.ruoyi.common.core.web.domain.AjaxResult)2 Log (com.ruoyi.common.log.annotation.Log)2 LoginUser (com.ruoyi.system.api.model.LoginUser)2