简体   繁体   English

如何使用jQuery将多个文件上传到Java控制器

[英]How to Upload Multiple files using jQuery to java controller

Hy all, 大家好

i am trying to create a workaround because of a jersey bug. 由于球衣错误,我正在尝试创建解决方法。 The jersey truncating the slashes from filename. 球衣将文件名中的斜杠截断。 So i would like to send my files up by another way. 所以我想通过另一种方式发送我的文件。 For 1 file i found a solution, this: 对于1个文件,我找到了解决方案,这是:

function extractFilename(s){ 
  // returns string containing everything from the end of the string 
  //   that is not a back/forward slash or an empty string on error
  //   so one can check if return_value===''
  return (typeof s==='string' && (s=s.match(/[^\\\/]+$/)) && s[0]) || '';
} 
<input type="file" onchange="alert(extractFilename(this.value));">

but with multiple files it does not work. 但是有多个文件则无法使用。 I tried to send them up via post: UI: 我试图通过发布:UI:

<input type="file" id="files" name="files" multiple="multiple" />

JS: JS:

function showUploadPanel() {

            var files = $("#files")[0].files;
       var form = createFormWithInputs("/user/document_upload/", files);

             form.appendTo( document.body ).submit();
         }

function createFormWithInputs(path, params) {
            var form = $(document.createElement( "form" ))
                .attr( {"method": "post", "action": path} );

            $.each( params, function(key,value){
                $.each( value instanceof Array? value : [value], 
                 function(i,val){
                    $(document.createElement("input"))
                        .attr({ "type": "hidden", "name": "files", "value": 
                        val })
                        .appendTo( form );
                }); 
            }); 

            return form;
        }

Controller: 控制器:

  @POST
  @Path("/document_upload")
  @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
  public Response documentUpload(@FormParam("files") List<File> files) {

I can have the files but they are empty '[object File]'. 我可以拥有文件,但是它们是空的[[object File]'。 There is no any content. 没有任何内容。

What do i do worng? 我该怎么办?

Thank you in advance, Endre 预先感谢您,恩德雷

You can try using Jquery plugin uploadify inorder to upload multiple file . 您可以尝试使用Jquery插件uploadify来上传多个文件。 http://www.uploadify.com/demos/ http://www.uploadify.com/demos/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM