gpt4 book ai didi

javascript - 如何使用 jQuery 将多个文件上传到 java Controller

转载 作者:太空宇宙 更新时间:2023-11-04 10:40:27 24 4
gpt4 key购买 nike

大家好,

由于 Jersey 错误,我正在尝试创建一个解决方法。 Jersey chop 了文件名中的斜杠。所以我想通过另一种方式发送我的文件。对于 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;
}

Controller :

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

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

我该怎么办?

预先感谢您,恩德烈

最佳答案

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

关于javascript - 如何使用 jQuery 将多个文件上传到 java Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49036108/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com