我正在尝试使用 ExtJs 和 Spring 上传文件(excel),但没有成功,所以我希望你能帮助我。在面板中,我有一个按钮(文件上传字段),我可以通过该按钮选择一个要上传的文件。
.
.
,{
xtype: 'fileuploadfield',
buttonOnly: true,
hideLabel: true,
buttonText: "Importuoti excel failą",
border: false,
itemId: "uploadBtn",
name: 'file'
},
.
.
这是我的控制器。每次我使用 fileuploadfield 选择文件时,它都会激活 uploadFile() 函数。
init: function() {
var me = this;
this.listen({
'#uploadBtn': {
'change': function(fld, value) {
console.log(value);
this.getTurtasPanel().setLoading(true, true);
if(value != ""){
me.uploadFile();
fld.reset();
}
}
},
})
上传文件函数。
uploadFile: function(){
var fp = Ext.create('Ext.form.Panel', {
fileUpload: true,
autoHeight: true,
bodyStyle: 'padding: 10px 10px 0 10px;',
labelWidth: 50,
method: 'POST',
defaults: {
anchor: '95%',
allowBlank: false,
msgTarget: 'side'
}
})
if(fp.getForm().isValid()){
fp.getForm().submit({
url: Turtas.Properties.getServicePath()+'/save/' + record.data.resource,
headers: {'Content-Type':'multipart/form-data; charset=UTF-8'},
})
}
},
和弹簧控制器:
@RequestMapping(value="turtas/save/gelezinkeliai", method=RequestMethod.POST)
public @ResponseBody void saveGelezinkeliaiFromExcel(@RequestParam("file") MultipartFile file){
System.out.println(file);
}
我得到的错误是:
Request processing failed; nested exception is org.springframework.web.multipart.MultipartException: Could not parse multipart servlet request; nested exception is java.io.IOException: org.apache.tomcat.util.http.fileupload.FileUploadException: the request was rejected because no multipart boundary was found
the request was rejected because no multipart boundary was found
我认为问题出在我的客户端请求中。我认为我的请求未附加我要上传的文件。我尝试将客户端中的标头设置为未定义,但随后内容类型变为“application/json”,并且出现请求不是多部分的错误。那么我的代码有什么问题吗?我真的希望有人能帮助我解决这个问题。提前致谢 !
Update
感谢@Lorenz Meyer 的回答,但现在我得到了不同的错误:
java.lang.IllegalArgumentException:您的InputStream既不是
OLE2 流,也不是 OOXML 流
我稍微改变了我的源代码。
@RequestMapping(value="turtas/save/gelezinkeliai", method=RequestMethod.POST)
public @ResponseBody void saveGelezinkeliaiFromExcel(@RequestParam("file") MultipartFile file){
System.out.println(file.getContentType());
System.out.println(file.getSize());
System.out.println(file.getName());
System.out.println(file.getOriginalFilename());
System.out.println(file.isEmpty());
}
Output:
application/octet-stream
0
file
true
从客户端我不包含参数,从服务器端我请求一个参数(文件参数),也许这就是问题所在。错误说我选择了错误的格式文件,尽管我选择了 .xlsx 或 .xls,并且出现了相同的错误。