Javascript 如何使用JQuery Dropzone将额外参数传递给我的java控制器方法
我有一个dropzone函数来上传pdf文件,并在我的控制器方法中接收它以处理和保存在数据库中,但我需要传递一个额外的参数,它是select中的值,我已经传递了它,但在控制器方法中接收空值 这是mi JQuery dropzone方法:Javascript 如何使用JQuery Dropzone将额外参数传递给我的java控制器方法,javascript,java,jquery,spring,dropzone,Javascript,Java,Jquery,Spring,Dropzone,我有一个dropzone函数来上传pdf文件,并在我的控制器方法中接收它以处理和保存在数据库中,但我需要传递一个额外的参数,它是select中的值,我已经传递了它,但在控制器方法中接收空值 这是mi JQuery dropzone方法: ... $("div#uploadFile").dropzone({ url: "/demo/uploadFile" ,params: {
...
$("div#uploadFile").dropzone({
url: "/demo/uploadFile"
,params: {
name: $( "#selectedName" ).val()
}
,acceptedFiles: ".pdf"
,clickable: false
,maxFilesize: 100
,addRemoveLinks: true
,init: function() {
this.on('addedfile', function(file) {
//i can see the value in alert
alert($( "#selectedName" ).val());
});
this.on('success', function(file, json) {
alert("ok");
});
});
尝试在requestParam中接收的控制器方法:
@RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
public ResponseEntity<?> uploadFile( @RequestParam("name") String name, MultipartFile file) {
//here print parameter and is NULL
System.out.println("My extra param received is : "+ name);
//process the file-pdf
...
....
}
有什么想法吗?提前谢谢 您可以在JS端将任何文件转换为字节数组,并在java端对其进行解码,因此您可以发送字节数组而不是文件。在您的JS端使用类似的方法您应该向此JS方法添加承诺:
function getBufferArray(file) {
var fr = new FileReader();
fr.readAsArrayBuffer(file);
return new Uint8Array(fr.result);
}
在java方面:
public class BASE64DecodedMultipartFile implements MultipartFile {
private final byte[] imgContent;
public BASE64DecodedMultipartFile(byte[] imgContent) {
this.imgContent = imgContent;
}
@Override
public String getName() {
// TODO - implementation depends on your requirements
return null;
}
@Override
public String getOriginalFilename() {
// TODO - implementation depends on your requirements
return null;
}
@Override
public String getContentType() {
// TODO - implementation depends on your requirements
return null;
}
@Override
public boolean isEmpty() {
return imgContent == null || imgContent.length == 0;
}
@Override
public long getSize() {
return imgContent.length;
}
@Override
public byte[] getBytes() throws IOException {
return imgContent;
}
@Override
public InputStream getInputStream() throws IOException {
return new ByteArrayInputStream(imgContent);
}
@Override
public void transferTo(File dest) throws IOException, IllegalStateException {
new FileOutputStream(dest).write(imgContent);
}
}
对于pharameters,请尝试添加到dropzone:
params: {
new_value: 'value'
},
你能试试吗
$("div#uploadFile").dropzone({
url: "/demo/uploadFile"
,acceptedFiles: ".pdf"
,clickable: false
,maxFilesize: 100
,addRemoveLinks: true
,init: function() {
this.on('addedfile', function(file) {
//i can see the value in alert
alert($( "#selectedName" ).val());
});
this.on("sending", function(file, xhr, formData) {
formData.append("name", $( "#selectedName" ).val());
console.log(formData);
});
this.on('success', function(file, json) {
alert("ok");
});
});
好的,但这不是问题所在,我已经处理了de文件并保存好了,只需要传递一个不是文件一部分的参数,我从一个选择框中获取参数,更新它,你可以不使用dropzone,按照我所说的将文件转换为字节数组,并在数据{}中使用常规ajax POST请求发送它,或者尝试添加参数:{}.updated mi post,我在dropzone中添加了参数,正如您所说,它可以工作,但我收到了空值,但是我在dropzone.addedFile函数中发出警告,我可以看到该值。使用$checkbox/select元素。is':selected';不起作用,我已经在dropzone函数之前使用了它,并在params中传递nameValue,但仍然打印null:var nameValue;$'selectedName'。在'change'上,函数{nameValue=$selectedName.val;};它起作用了!这种方式在dopzone函数中不是必需的参数。谢谢