blueimp jquery文件上传空文件上传结果struts2,fileitems为空 [英] blueimp jquery file upload Empty file upload result struts2, fileitems empty

查看:150
本文介绍了blueimp jquery文件上传空文件上传结果struts2,fileitems为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Blueimp和服务器端是Java,Struts2。我无法找到使用Java的例子,无论如何,我设法使用示例代码,但我正在获取空文件上传结果当我试图上传一个单一的文件也。 HTML部分是一样的,我不粘贴在这里,因为它可能会很长。



jQuery是:

  $(document).ready(function(){
'use strict';

//初始化jQuery文件上传部件:
$('#fileupload')。fileupload ();

//通过重定向选项启用iframe跨域访问:
$('#fileupload')。fileupload(
'option',
'重定向',
window.location.href.replace(
/ \ / [^ \ /] * $ /,
'/cors/result.html?%s'



if(window.location.hostname ==='blueimp.github.com'){
//演示设置:
$ ('#fileupload')。fileupload('option',{
url:'//jquery-file-upload.appspot.com/',
maxFileSize:5000000,
acceptFileTypes:/ (\。| \ /)(gif | jpe?g | png)$ / i,
process:[
{
action:'load ,
fileTypes:/ ^ image \ /(gif | jpeg | png)$ /,
maxFileSize:20000000 // 20MB
},
{
操作: 'resize',
maxWidth:1440,
maxHeight:900
},
{
action:'save'
}
]
});
//上传服务器状态检查支持CORS的浏览器:
if($ .support.cors){
$ .ajax({
url:'// jquery-file -upload.appspot.com/',
type:'HEAD'
})。fail(function(){
$('< span class =alert alert-error/ >')
.text('Upload server currently unavailable - '+
new Date())
.appendTo('#fileupload');
});
}
} else {
//加载现有文件:
$('#fileupload')。each(function(){
var that = this;
$ .getJSON(this.action,function(result){
if(result&& result.length){
$(that).fileupload('option','done' )
.call(that,null,{result:result});
}
});
});
}

});

动作:

  @Namespace(/)
@InterceptorRefs({
@InterceptorRef(fileUpload),
@InterceptorRef(basicStack )
})
public class UploadAction extends ActionSupport implements ServletRequestAware,ServletResponseAware {

HttpServletRequest req;
HttpServletResponse res;
// private File fileUploadPath = new File(c:\\temp\\);
私人列表< File> uploads = new ArrayList< File>();
私人清单< String> uploadFileNames = new ArrayList< String>();
私人清单< String> uploadContentTypes = new ArrayList< String>();

公共列表< File> getUploads(){
返回上传;

$ b $ public void setUploads(List< File> uploads){
this.uploads = uploads;
}

public List< String> getUploadFileNames(){
return uploadFileNames;
}

public void setUploadFileNames(List< String> uploadFileNames){
this.uploadFileNames = uploadFileNames;
}

public List< String> getUploadContentTypes(){
return uploadContentTypes;
}

public void setUploadContentTypes(List< String> uploadContentTypes){
this.uploadContentTypes = uploadContentTypes;

$ b $ @Action(value =upload,results = {@Result(name =success,type =json)
})
public String uploadFiles()throws IOException
{
System.out.println(upload1);
System.out.println(files:);
(File u:uploads){
System.out.println(***+ u +\ t+ u.length());
}
System.out.println(filenames:);
for(String n:uploadFileNames){
System.out.println(***+ n);
}
System.out.println(content types:);
for(String c:uploadContentTypes){
System.out.println(***+ c);
}
System.out.println(\\\
\\\
);
if(!ServletFileUpload.isMultipartContent(req)){
throw new IllegalArgumentException(请求不是多部分,请为您的表单请求'multipart / form-data'enctype。
}
返回SUCCESS;
}

@Override
public void setServletRequest(HttpServletRequest hsr){
this.req = hsr;
}

@Override
public void setServletResponse(HttpServletResponse hsr){
this.res = hsr;





$正如我所说,这个动作文件,但我仍然得到所有文件的空值,在Firebug的GET响应中,我看到请求不是多部分,请为您的表单multipart / form-data'enctype



感谢&问候。

解决方案

您可以使用 fileUpload 拦截器来解析你的 multipart / form-data 请求。它使用与 commons-fileupload 实现。 apache.org/maven/struts2-core/apidocs/org/apache/struts2/dispatcher/multipart/MultiPartRequestWrapper.htmlrel =nofollow> MultipartRequestWrapper 准备Struts2调度程序的操作。更多关于如何上传的例子,你可以在这里找到


I am using Blueimp and server side is Java, Struts2. I couldn't find examples using Java, anyway I managed to use the sample code, but I am getting "Empty file upload result" when I am trying to upload a single file also. The HTML part is the same, I am not pasting here as it may go lengthy.

The jQuery is:

$(document).ready(function () {
    'use strict';

    // Initialize the jQuery File Upload widget:
    $('#fileupload').fileupload();

    // Enable iframe cross-domain access via redirect option:
    $('#fileupload').fileupload(
        'option',
        'redirect',
        window.location.href.replace(
            /\/[^\/]*$/,
            '/cors/result.html?%s'
        )
    );

    if (window.location.hostname === 'blueimp.github.com') {
        // Demo settings:
        $('#fileupload').fileupload('option', {
            url: '//jquery-file-upload.appspot.com/',
            maxFileSize: 5000000,
            acceptFileTypes: /(\.|\/)(gif|jpe?g|png)$/i,
            process: [
                {
                    action: 'load',
                    fileTypes: /^image\/(gif|jpeg|png)$/,
                    maxFileSize: 20000000 // 20MB
                },
                {
                    action: 'resize',
                    maxWidth: 1440,
                    maxHeight: 900
                },
                {
                    action: 'save'
                }
            ]
        });
        // Upload server status check for browsers with CORS support:
        if ($.support.cors) {
            $.ajax({
                url: '//jquery-file-upload.appspot.com/',
                type: 'HEAD'
            }).fail(function () {
                $('<span class="alert alert-error"/>')
                    .text('Upload server currently unavailable - ' +
                            new Date())
                    .appendTo('#fileupload');
            });
        }
    } else {
        // Load existing files:
        $('#fileupload').each(function () {
            var that = this;
            $.getJSON(this.action, function (result) {
                if (result && result.length) {
                    $(that).fileupload('option', 'done')
                        .call(that, null, {result: result});
                }
            });
        });
    }

});

The action:

@Namespace("/")
@InterceptorRefs({
  @InterceptorRef("fileUpload"),
  @InterceptorRef("basicStack")
})
public class UploadAction extends ActionSupport implements ServletRequestAware, ServletResponseAware{

    HttpServletRequest req;
    HttpServletResponse res;
  //  private File fileUploadPath=new File("c:\\temp\\");
    private List<File> uploads = new ArrayList<File>();
    private List<String> uploadFileNames = new ArrayList<String>();
    private List<String> uploadContentTypes = new ArrayList<String>();

    public List<File> getUploads() {
        return uploads;
    }

    public void setUploads(List<File> uploads) {
        this.uploads = uploads;
    }

    public List<String> getUploadFileNames() {
        return uploadFileNames;
    }

    public void setUploadFileNames(List<String> uploadFileNames) {
        this.uploadFileNames = uploadFileNames;
    }

    public List<String> getUploadContentTypes() {
        return uploadContentTypes;
    }

    public void setUploadContentTypes(List<String> uploadContentTypes) {
        this.uploadContentTypes = uploadContentTypes;
    }

    @Action(value="upload", results = { @Result(name="success", type="json")
    })
    public String uploadFiles() throws IOException
    {
        System.out.println("upload1");
        System.out.println("files:");
        for (File u: uploads) {
            System.out.println("*** "+u+"\t"+u.length());
        }
        System.out.println("filenames:");
        for (String n: uploadFileNames) {
            System.out.println("*** "+n);
        }
        System.out.println("content types:");
        for (String c: uploadContentTypes) {
            System.out.println("*** "+c);
        }
        System.out.println("\n\n");
        if (!ServletFileUpload.isMultipartContent(req)) {
            throw new IllegalArgumentException("Request is not multipart, please 'multipart/form-data' enctype for your form.");
        }
        return SUCCESS;
    }

    @Override
    public void setServletRequest(HttpServletRequest hsr) {
        this.req=hsr;
    }

    @Override
    public void setServletResponse(HttpServletResponse hsr) {
        this.res=hsr;
    }

}

As I said, I have changed the action file, but I still get all empty values for files, and in the Firebug's GET response I see "Request is not multipart, please 'multipart/form-data' enctype for your form"

Thanks & Regards.

解决方案

You may use fileUpload interceptor to parse your "multipart/form-data" requests. It uses the same commons-fileupload implementation wrapped by the MultipartRequestWrapper in prepare operations by the Struts2 dispatcher. More about how to file upload with examples you could find here.

这篇关于blueimp jquery文件上传空文件上传结果struts2,fileitems为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆