我正在尝试使用模态窗口中的js保存文件。我把它保存好了然后我被重定向到只写有“ok”的页面。 我的意思是:
我的js看起来像:
var name = $('#upload-track-name').val();
var description = $('#upload-track-description').val();
var file = document.getElementById('input-file').files[0];
var isValidate = true;
if (name.trim() == '') {
$('#error-upload-track-name').show();
isValidate = false;
}
if (!file) {
$('#error-upload-track-file').show();
isValidate = false;
}
if (!isValidate) {
return;
}
$.ajax({
url: '/tracks/upload',//I see this empty page on this url
type: 'post',
data: {
file: file,
name: name,
description: description
},
success: function (data){
if (data == 1) {
$('#error-upload-track-file').hide();
$('#error-upload-track-name').show();
} else if (data == 2) {
$('#error-upload-track-name').hide();
$('#error-upload-track-file').show();
} else {
$('#modal-upload-success').show();
}
})
这是我的@Controller:
@ResponseBody
@RequestMapping(value = TracksGeopointsRoutes.UPLOAD, method = RequestMethod.POST)
public String uploadTrack(@RequestParam MultipartFile file,
@RequestParam String name,
@RequestParam(required = false, defaultValue = "") String description,
Model model){
TracksGeopointsDoc tracksGeopointsDoc = new TracksGeopointsDoc();
if (name.equals("")) {
return "1";
}
if (file.getSize() == 0 || file == null){
return "2";
}
ObjectId fileId = fileService.saveWithDelete(file, null);
tracksGeopointsDoc.setFile(fileId);
tracksGeopointsDoc.setUploadDate(new Date());
tracksGeopointsDoc.setName(name);
tracksGeopointsDoc.setDescription(description);
tracksGeopointsService.saveTrack(tracksGeopointsDoc);
try {
File convFile = tracksGeopointsService.convert(file);
String s = convFile.getAbsolutePath();
mySaxParser.setUpMySaxParser(convFile.getAbsolutePath(), tracksGeopointsDoc.getId(), convFile.getName());
} catch (IOException e) {
e.printStackTrace();
}
return "ok";
}
我怀疑这是因为@ResponseBody注释。如果我是对的,任何人都可以告诉我如何避免这种情况。