NutzCN Logo
问答 文件上传,AdaptorErrorContext错误信息为空,用了拦截器
发布于 1972天前 作者 herexu 1309 次浏览 复制 上一个帖子 下一个帖子
标签:
    @At
    @POST
    @Ok("json")
    @AdaptBy(type = UploadAdaptor.class, args = {"ioc:imageUpload"})
    @RequiresAuthentication
    @ApiOperation(value = "文件上传", notes = "文件上传", httpMethod = "POST")
    @ApiParam(value = "图片接受参数", name = "tf")
    public Object image(@Param("Filedata") TempFile tf, HttpServletRequest req, AdaptorErrorContext err) {
        try {
            if (err != null && err.getAdaptorErr() != null) {
                return ApiResult.error("文件不合法,支持(gif|jpg|png)");
            } else if (tf == null) {
                return ApiResult.error("空文件");
            } else {
                String suffix = tf.getSubmittedFileName().substring(tf.getSubmittedFileName().indexOf("."));
                String f = Globals.AppUploadPath + "image/" + DateUtil.format(new Date(), "yyyyMMdd") + "/" + R.UU32() +suffix ;
                Files.write(new File(f), tf.getInputStream());
                Document document = new Document() ;
                document.setUrl("/platform/file/download/fileDownload?url="+f);
                document.setPath(f);
                document.setName(tf.getSubmittedFileName());
                documentService.insert(document) ;
                return ApiResult.success(document);
            }
        } catch (Exception e) {
            return ApiResult.error("系统错误");
        } catch (Throwable e) {
            return ApiResult.error("图片格式错误");
        }
    }

上传文件格式及大小验证,err里面的errros是空的,只能拿到err.getAdaptorErr()里面的一个Unsupport file name 'test.pdf'

4 回复

在upload.js里面限制了文件后缀吧

在前端JS详细校验,后端只校验合法不合法,不合法,就只提示不合法即可,意思是这样吗

没懂你的意思,我只是解释一下为何提示文件名不支持,原因是upload.js里面配置的允许的文件后缀,没有包含pdf

添加回复
请先登陆
回到顶部