繁体   English   中英

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

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

大家好

由于球衣错误,我正在尝试创建解决方法。 球衣将文件名中的斜杠截断。 所以我想通过另一种方式发送我的文件。 对于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));">

但是有多个文件则无法使用。 我试图通过发布: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]'。 没有任何内容。

我该怎么办?

预先感谢您,恩德雷

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

暂无
暂无

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

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