【问题标题】:How to Upload Multiple files using jQuery to java controller如何使用 jQuery 将多个文件上传到 java 控制器
【发布时间】:2018-02-28 18:20:05
【问题描述】:

大家好,

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

但是对于多个文件,它不起作用。我试图通过邮寄方式将它们发送出去: 用户界面:

<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) {

我可以拥有这些文件,但它们是空的“[目标文件]”。没有任何内容。

我该怎么做?

提前谢谢你, 恩德雷

【问题讨论】:

    标签: javascript java jquery jersey


    【解决方案1】:

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

    【讨论】:

    • 它对我不起作用。你能附上来源来表达你的想法吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-06
    • 2014-09-21
    • 2014-05-04
    • 2017-08-16
    • 1970-01-01
    • 2017-10-04
    相关资源
    最近更新 更多