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

问题描述 投票:1回答:1

他全部,

我正试图创建一个解决方法,因为一个球衣错误。泽西从文件名截断斜线。所以我想通过另一种方式发送我的文件。对于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));">

但是有多个文件它不起作用。我试图通过post发送它们:UI:

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

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;
        }

控制器:

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

我可以拥有这些文件,但它们是空的'[object File]'。没有任何内容。

我做错了什么?

恩迪尔,提前谢谢你

javascript java jquery jersey
1个回答
0
投票

您可以尝试使用Jquery插件uploadify来上传多个文件。 http://www.uploadify.com/demos/

© www.soinside.com 2019 - 2024. All rights reserved.