我正在尝试将文件对象数组传递给 spring MVC 控制器。但我收到 400 Bad request 错误。
在我的 jsp 中,我编写了一个 jquery 方法来获取文件数组。
function getFilelist() {
var files = $('body').data('filelist');
return files;
}
并使用 post Json 方法上传这些所有文件。
- -更新 - -
我正在添加整个 init() 方法:
function init() {
$('input:button').button();
$('#submit').button();
$('#uploadForm').submit(function(event) {
event.preventDefault();
alert("uploading..");
$.postJSON('fileSave.htm', {
filename: getFilelist()
},
function(result) {
if (result.success == true) {
dialog('Success', 'Files have been uploaded!');
} else {
dialog('Failure', 'Unable to upload files!');
}
});
});
$('#reset').click(function() {
clearForm();
dialog('Success', 'Fields have been cleared!');
});
$('#upload').fileupload({
dataType: 'json',
done: function (e, data) {
$.each(data.result, function (index, file) {
$('body').data('filelist').push(file);
$('#filename').append(formatFileDisplay(file));
$('#attach').empty().append('Add another file');
});
}
});
$("#attach").click(function () {
$("#upload").trigger('click');
});
$('body').data('filelist', new Array());
}
这里 getFilelist() 是文件对象的数组。在我的控制器中,我写道:
@RequestMapping(value="fileSave", method=RequestMethod.POST)
public @ResponseBody StatusResponse message(
@RequestParam("filesAttachedList") FilesAttachedList filesAttachedList) {
// some code here
}
我的FilesAttachedList
班级:
public class FilesAttachedList implements Serializable {
private static final long serialVersionUID = 5944305104394883501L;
private List<FileAttachment> filesList;
public List<FileAttachment> getFilesList() {
return filesList;
}
public void setFilesList(List<FileAttachment> filesList) {
this.filesList = filesList;
}
@Override
public String toString() {
return "FilesAttachedList [filesList=" + filesList + "]";
}
}
那么如何将对象数组从 jquery 传递给控制器呢?
- 更新 -
当我检查浏览器 getFileList 返回
Object
attachableId: null
attachableType: null
attachmentContentType: null
attachmentFileName: "http://localhost:8080/myproject/resources/images.jpg"
attachmentFileSize: 6994
attachmentUpdatedAt: null
createdAt: null
creatorId: null
id: null
name: "images.jpg"
updatedAt: null
updaterId: null
__proto__: Object
length: 1
__proto__: Array[0]
这就是如果我添加两个图像对象
Array[2]
0: Object
1: Object
length: 2
__proto__: Array[0]
----更新3 ---
当我从@RequestParam("filesAttachedList") FilesAttachedList filesAttachedList
to更改@RequestBody final String filesAttachedList
并添加两个图像时,它给了我以下字符串对象。我能读懂这个字符串吗?
{
"filename": [
{
"id": null,
"createdAt": null,
"updatedAt": null,
"name": "images.jpg",
"attachableId": null,
"attachableType": null,
"attachmentFileName": "http:\/\/localhost:8080\/myproject\/resources\/images.jpg",
"attachmentContentType": null,
"attachmentFileSize": 6994,
"attachmentUpdatedAt": null,
"creatorId": null,
"updaterId": null
},
{
"id": null,
"createdAt": null,
"updatedAt": null,
"name": "lion.jpg",
"attachableId": null,
"attachableType": null,
"attachmentFileName": "http:\/\/localhost:8080\/myproject\/resources\/lion.jpg",
"attachmentContentType": null,
"attachmentFileSize": 36670,
"attachmentUpdatedAt": null,
"creatorId": null,
"updaterId": null
}
]
}