【发布时间】:2011-05-22 15:05:04
【问题描述】:
我需要通过单击模式窗口上的 Ajax 按钮来执行文件上传。我遇到了问题,因为文件已上传,但上传过程尚未完成,无法使用文件名更新表单。有文件上传后无法隐藏的AjaxIndicator。模态窗口被实现为面板。代码如下:
uploadFile=new IndicatingAjaxButton("uploadFile"){
private static final long serialVersionUID = 1L;
@Override
protected void onSubmit(AjaxRequestTarget target, Form<?> form)
{
uploadFile.getAjaxIndicatorMarkupId();
FileUpload fileUpload =
((FileUploadFieldPanel)uploadPanel).getUploadField().getFileUpload();
if (fileUpload != null)
{
String fileName = fileUpload.getClientFileName();
String path = uploadpath + relativeuploadpath;
File newFile = new File(path, fileName);
checkFileExists(newFile);
try {
newFile.createNewFile();
fileUpload.writeTo(newFile);
}
catch (IOException e) {
e.printStackTrace();
}
titleField.setModelObject(fileName);
target.addComponent(titleField);
}
};
uploadFile.setOutputMarkupId(true);
form.add(uploadFile);
问题是:如何用文件名更新表单上的titleField?在这种情况下,“target.addComponent(titleField);”不工作。
【问题讨论】:
-
我倾向于依靠 Form 的 onSubmit() 来处理表单数据(例如保存到数据库),并且只使用 AjaxButton 的 onSubmit() 方法来进行 ajax 操作(例如选择哪些组件要隐藏等)。无论如何,我认为 titleField 仍然在页面上?您没有将它与表单或其他任何内容一起隐藏吗?
标签: ajax file-upload wicket