我有一个ajax电话:
发生的事情是我点击了一个点击“输入文件”的图片,我不想让它显示出来。当输入val()被更改时,方法uploadListener()
被调用,这基本上是一个ajax方法,将文件送到服务器“上传到服务器”
代码是这样的:
HTML:
<img id="${assayAssessRequest.id}" src="[@spring.url '/images/buttons/upload.jpg'/]" onclick="uploadFile(this);" title="Upload File" />
<form id="uploadForm" action="[@spring.url '/assay/designability/uploadFile.htm'/]" method="POST" enctype="multipart/form-data">
<div style="display: none;">
[@spring.formInput path="multiPartBean.file" fieldType="file" attributes="title='path' class='upload' accept='.txt,.csv,.zip'" /]
[@spring.formHiddenInput path="multiPartBean.fileName" attributes=" onchange='uploadListener();'class='uploadFileName'" /]
[@spring.bind "multiPartBean"/]
</div>
<input type="submit" id="uploadButton" value="upload" />
</form>
JavaScript的:
function uploadFile(){
document.getElementById('inputFile').click();
}
function uploadListener(){
$('#uploadForm').attr("action","[@spring.url '/assay/designability/uploadFile.htm'/]");
alert($('#uploadForm').attr('action'));
this.document.getElementById("uploadForm").submit = true;
alert("After Submit");
return false;
}
服务器控制器:
@Controller
@PreAuthorize("isAuthenticated()")
@RequestMapping("/assay/designability")
@SessionAttributes({"assayAssessmentsInitializersBean","assayAssessmentsRequestsDetailsBean"})
public class AssayDesignabilityController extends AssayBaseController {
@RequestMapping(value = "/uploadFile",method= RequestMethod.GET)
public String uploadFile(@RequestParam(value = "file")Object file){
MultipartFile multipartFile=(MultipartFile)file;
logger.info(multipartFile.getName());
return multipartFile.getName();
}
}
现在当我做这一切时,响应给了我废话,当我尝试调试时,我从未接触过控制器方法。任何帮助??
修改 现在我尝试提交它我已更新代码,我有相同的行为没有回复。
答案 0 :(得分:2)
我通过避免ajax调用解决了我的问题,并通过@Ian重新实现我的代码。
这是代码:
Html plus Freemarker
<form id="uploadForm" action="#" method="POST" enctype="multipart/form-data">
<div class="instruction popup_inst">
<span class="popup_logo">[@spring.message "pandaLogo"/]</span>
<div class="float_right">
<input type="button" id="cancelBtn" class="btn" onclick="refreshParentTable();closePopup();" value="[@spring.message "cancelButton"/]" />
<input class="btn" type="submit" id="submit" onclick="validateFileInput();" value="[@spring.message "uploadButton"/]" />
</div>
</div>
<span class="popup_title">[@spring.message "uploadFile"/]</span>
<div class="popup_container">
[@spring.bind "assayAssessmentsRequestBean"/]
[@spring.formInput path="assayAssessmentsRequestBean.designabilityFile.file" fieldType="file" attributes="title='path' class='upload' accept='.txt,.csv,.zip'" /]
[@spring.formHiddenInput path="assayAssessmentsRequestBean.designabilityFile.fileName" attributes="class='uploadFileName'" /]
[@spring.formHiddenInput path="assayAssessmentsRequestBean.dateOfAssessment" attributes="" /]
[@spring.formHiddenInput path="assayAssessmentsRequestBean.id" attributes="" /]
</div>
<input id="uploadfile" type="hidden" value="${uploadfile}"/>
</form>
控制器:
@RequestMapping(value = "/uploadFile",method= RequestMethod.POST)
public ModelAndView uploadFile(@ModelAttribute(value = "assayAssessmentsRequestBean")AssayAssessmentsRequestBean assayAssessmentsRequestBean,HttpSession session) throws PanDaApplicationException {
//mycode
}
从这次经历中我会建议evreyone避免通过ajax调用来发送文件给我
我使用弹出窗口创建表单并提交上传操作。
谢谢大家试图帮助
答案 1 :(得分:1)
如果我看得正确,提交操作指向“uploadfile.HTM”
$('#uploadForm').attr("action","[@spring.url '/assay/designability/uploadFile.htm'/]");
但控制器中的方法仅映射到uploadFile(不带.htm)
@RequestMapping(value = "/uploadFile",method= RequestMethod.GET)
也许你的web.xml / servlet-spring.xml上有另一个配置可以实现这一点,但除此之外,这可能就是问题所在。
希望这有帮助
此致