如何解决如何使用JQuery Dropzone将额外参数传递给我的Java控制器方法
我有一个dropzone函数来上载pdf文件,并以我的控制器方法接收它以处理并保存在数据库中,但是我需要传递一个额外的参数,该参数是select的值,我已经传递了,但是我收到了“ null”值在控制器方法中。
这是mi JQuery dropzone方法:
...
$("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
...
....
}
有什么主意吗?预先感谢!
解决方法
您可以尝试:
$("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',json) {
alert("ok");
});
});
,
您可以在JS端将任何文件转换为字节数组,并在Java端对其进行解码,因此您可以发送字节数组而不是文件。在JS端使用类似的方法(您应在此JS方法中添加Promise):
<?xml version="1.0" encoding="UTF-8"?>
在Java方面:
<groupId>com.example</groupId>
<artifactId>secondApp</artifactId>
<version>1.0-SNAPSHOT</version>
<name>secondApp</name>
<packaging>war</packaging>
<properties>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<junit.version>5.6.2</junit.version>
</properties>
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>8.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>5.2.9.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.2.9.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.9.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.2.9.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>5.2.9.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.2.9.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.9.RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.3.0</version>
</plugin>
</plugins>
</build>
对于法老,请尝试添加到您的放置区:
function getBufferArray(file) {
var fr = new FileReader();
fr.readAsArrayBuffer(file);
return new Uint8Array(fr.result);
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。