NutzCN Logo
问答 layui图片上传后台封装不上问题
发布于 2535天前 作者 蛋蛋的忧伤 5142 次浏览 复制 上一个帖子 下一个帖子
标签:

layui代码:

  //普通图片上传
                        var uploadInst = upload.render({
                            elem: file
                            ,field:"image"               
                            ,url: '${base}/upload/image'
							,data:{"path":path}
                            ,before: function(obj){
                                //预读本地文件示例,不支持ie8
                                obj.preview(function(index, file, result){
                                    $(file).attr('src', result); //图片链接(base64)
                                });
                            }
                            ,done: function(res){
                                console.log(res);
                                //如果上传失败
                                if(res.code == 1 ){
                                    _this.val(data.url);
                                }else{
                                    _this.val("");
                                    return layer.msg('上传失败');
                                }
                                //上传成功
                            }
                            ,error: function(){
                                //演示失败状态,并实现重传
                                return layer.msg('网络异常,上传失败!');
                            }
                        });

后台代码

 @At
    @POST
    @AdaptBy(type = UploadAdaptor.class, args = {"ioc:imageUpload"})
    @Ok(HtmlViewMaker.VIEW_JSON)
    public Object image(@Param("image") TempFile file, @Param("path") String path)
            throws IOException, ServletException {
        if (Strings.isBlank(path))
            path = "image/" + format;
        UploadImage image = new UploadImage(file);
        image.setContent_path(UploadConfig.CONTENT_PATH_DIR);
        image.setDir(UploadConfig.DIR);
        String url = image.save(parse(path));
        if (!UploadConfig.CONTENT_PATH.equals(UploadConfig.CONTENT_PATH_DIR))
            url = url.replace(UploadConfig.CONTENT_PATH_DIR, UploadConfig.CONTENT_PATH);
        Result result = Result.ok();
        result.put("url", url);
        return result;
    }

进来后file参数为null,不知道为什么,我已经指定了layuid 的field参数啊,为啥还不行?
chrome拦截网络请求提交参数为:

------WebKitFormBoundaryNxYYCVXeFrhyDqHa
Content-Disposition: form-data; name=""; filename="2.png"
Content-Type: image/png


------WebKitFormBoundaryNxYYCVXeFrhyDqHa
Content-Disposition: form-data; name="path"


------WebKitFormBoundaryNxYYCVXeFrhyDqHa--
10 回复

后台解析日志贴一下

[DEBUG] 17:31:32.294 org.nutz.mvc.impl.UrlMappingImpl.get(UrlMappingImpl.java:101) - Found mapping for [POST] path=/upload/image : UploadController.image(UploadController.java:35)
[DEBUG] 17:31:32.307 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:151) - Get 'uploadController'<class com.wangzc.mvc.controller.UploadController>
[DEBUG] 17:31:32.308 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:177) - 	 >> Load definition name=uploadController
[DEBUG] 17:31:32.308 org.nutz.ioc.loader.combo.ComboIocLoader.load(ComboIocLoader.java:161) - Found IocObject(uploadController) in AnnotationIocLoader(packages=[com.yixin.njboss, com.wangzc])
[DEBUG] 17:31:32.310 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:209) - 	 >> Make...'uploadController'<class com.wangzc.mvc.controller.UploadController>
[DEBUG] 17:31:32.311 org.nutz.ioc.aop.impl.DefaultMirrorFactory.getMirror(DefaultMirrorFactory.java:70) - Load class com.wangzc.mvc.controller.UploadController without AOP
[DEBUG] 17:31:32.311 org.nutz.ioc.impl.ScopeContext.save(ScopeContext.java:64) - Save object 'uploadController' to [app] 
[DEBUG] 17:31:32.324 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:151) - Get 'conf'<>
[DEBUG] 17:31:32.332 org.nutz.mvc.upload.UploadAdaptor.getReferObject(UploadAdaptor.java:184) - Select Html4 Form upload parser --> /admin/upload/image
[DEBUG] 17:31:32.342 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:39) - FastUpload : /upload/image
[DEBUG] 17:31:32.343 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:54) - info created
[DEBUG] 17:31:32.344 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:60) - Params map created - 0 params
[DEBUG] 17:31:32.345 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:77) - boundary: 
------WebKitFormBoundaryu7PBKJNvgZzElmx2
[DEBUG] 17:31:32.350 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:100) - skip first boundary
[DEBUG] 17:31:32.350 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:111) - Reading...
[DEBUG] 17:31:32.351 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:133) - Upload File info: FilePath=[2.png],fieldName=[]
[DEBUG] 17:31:32.351 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:137) - Upload Info: name=2.png,content_type=image/png
[DEBUG] 17:31:32.388 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:133) - Upload File info: FilePath=[null],fieldName=[path]
[DEBUG] 17:31:32.389 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:214) - Found a param, name=[path] value=[]
[DEBUG] 17:31:32.389 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:231) - ...Done 159437 bytes readed
[DEBUG] 17:31:37.448 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:151) - Get 'sysLogService'<interface com.wangzc.mvc.service.SysLogService>
[DEBUG] 17:31:37.449 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:151) - Get 'sysUserService'<interface com.wangzc.mvc.service.SysUserService>
[DEBUG] 17:31:37.449 org.nutz.dao.impl.sql.run.NutDaoExecutor.printSQL(NutDaoExecutor.java:388) - SELECT * FROM sys_user  WHERE user_id=?
    | 1 |
    |---|
    | 1 |
  For example:> "SELECT * FROM sys_user  WHERE user_id=1"
[DEBUG] 17:31:37.462 org.nutz.dao.impl.sql.run.NutDaoExecutor.printSQL(NutDaoExecutor.java:388) - INSERT INTO sys_log(user_id,account,req_ip,req_uri,req_method,req_params,req_body,user_agent,log_desc,create_time) VALUES(?,?,?,?,?,?,?,?,?,?) 
    | 1 |     2 |               3 |                   4 |    5 |  6 | 7 |                                                                                                            8 | 9 |                  10 |
    |---|-------|-----------------|---------------------|------|----|--|--------------------------------------------------------------------------------------------------------------|--|---------------------|
    | 1 | admin | 0:0:0:0:0:0:0:1 | /admin/upload/image | POST | {} |  | Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.75 Safari/537.36 |  | 2017-11-21 17:31:37 |
  For example:> "INSERT INTO sys_log(user_id,account,req_ip,req_uri,req_method,req_params,req_body,user_agent,log_desc,create_time) VALUES('1','admin','0:0:0:0:0:0:0:1','/admin/upload/image','POST','{}','','Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.75 Safari/537.36','','2017-11-21 17:31:37') "
[DEBUG] 17:31:37.485 com.wangzc.mvc.processor.LogTimeProcessor.process(LogTimeProcessor.java:34) - [POST]URI=/admin/upload/image 5178ms
[WARN ] 17:31:37.486 org.nutz.mvc.impl.processor.FailProcessor.process(FailProcessor.java:28) - Error@/upload/image :
java.lang.NullPointerException
	at com.wangzc.mvc.upload.AbstractUpload.save(AbstractUpload.java:33)
	at com.wangzc.mvc.upload.UploadImage.save(UploadImage.java:56)
	at com.wangzc.mvc.controller.UploadController.image(UploadController.java:40)
	at com.wangzc.mvc.controller.UploadController$FM$image$29425211d1f32fcfbef0fd748550c63f.invoke(UploadController.java)
	at org.nutz.mvc.impl.processor.MethodInvokeProcessor.process(MethodInvokeProcessor.java:31)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.AdaptorProcessor.process(AdaptorProcessor.java:33)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.ActionFiltersProcessor.process(ActionFiltersProcessor.java:58)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at com.wangzc.mvc.processor.NutShiroProcessor.process(NutShiroProcessor.java:53)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.ModuleProcessor.process(ModuleProcessor.java:123)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.EncodingProcessor.process(EncodingProcessor.java:27)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.UpdateRequestAttributesProcessor.process(UpdateRequestAttributesProcessor.java:15)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at com.wangzc.mvc.processor.LogTimeProcessor.process(LogTimeProcessor.java:25)
	at org.nutz.mvc.impl.NutActionChain.doChain(NutActionChain.java:44)
	at org.nutz.mvc.impl.ActionInvoker.invoke(ActionInvoker.java:67)
	at org.nutz.mvc.ActionHandler.handle(ActionHandler.java:31)
	at org.nutz.mvc.NutFilter.doFilter(NutFilter.java:202)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
	at com.alibaba.druid.support.http.WebStatFilter.doFilter(WebStatFilter.java:123)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
	at org.apache.shiro.web.servlet.ProxiedFilterChain.doFilter(ProxiedFilterChain.java:61)
	at org.apache.shiro.web.servlet.AdviceFilter.executeChain(AdviceFilter.java:108)
	at org.apache.shiro.web.servlet.AdviceFilter.doFilterInternal(AdviceFilter.java:137)
	at org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125)
	at org.apache.shiro.web.servlet.ProxiedFilterChain.doFilter(ProxiedFilterChain.java:66)
	at org.apache.shiro.web.servlet.AbstractShiroFilter.executeChain(AbstractShiroFilter.java:449)
	at org.apache.shiro.web.servlet.AbstractShiroFilter$1.call(AbstractShiroFilter.java:365)
	at org.apache.shiro.subject.support.SubjectCallable.doCall(SubjectCallable.java:90)
	at org.apache.shiro.subject.support.SubjectCallable.call(SubjectCallable.java:83)
	at org.apache.shiro.subject.support.DelegatingSubject.execute(DelegatingSubject.java:383)
	at org.apache.shiro.web.servlet.AbstractShiroFilter.doFilterInternal(AbstractShiroFilter.java:362)
	at org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:94)
	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:504)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
	at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:620)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:502)
	at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1132)
	at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:684)
	at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1533)
	at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1489)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
	at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
	at java.lang.Thread.run(Thread.java:748)

name是空字符串?

加个 @Param("..") NutMap params 参数,然后打印一下它

{
   "": {
      "file": {
      },
      "meta": {
         "map": {
            "filename": "2.png",
            "Content-Disposition": "form-data",
            "name": "",
            "Content-Type": "image/png"
         }
      }
   },
   "path": ""
}

name的确是个空字符串.......为啥lei?

upload.render 文档在哪里呢?给个链接吧

问题解决了.....
我给input加个name属性就行了....这垃圾框架,我分明指定了field参数,他竟然还是空....明明说的是默认是file,妈的默认是个空

给它报个issue吧😊

我是用js自定义了一个input, var file = "" ;然后竟然就有这bug.....简直了...闲心这家伙都不经常在社区回复,真是差评...

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