【发布时间】:2014-08-14 04:47:25
【问题描述】:
标题可能会使问题看起来更老,但还有更多内容。
为了隐藏浏览按钮的文本框,我添加了以下代码。
<html:form action="/validate" enctype="multipart/form-data">
<html:file property="file" id="selectedFile" style="display: none;" />
<input type="button" value="Browse..." onclick="document.getElementById('selectedFile').click();" />
<html:sumbit/>
</html:form>
在选择文件之前效果很好。我正在为我的项目使用 spring。
当我使用此方法时,我无法将文件获取到类方法。我需要隐藏文本框。以下是我在课堂上使用的代码。
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
FileUploadForm uploadForm = (FileUploadForm) form;
FileOutputStream outputStream = null;
FormFile formFile = null;
try {
formFile = uploadForm.getFile();
String path = getServlet().getServletContext().getRealPath("") + "/" + formFile.getFileName();
outputStream = new FileOutputStream(new File(path));
outputStream.write(formFile.getFileData());
} finally {
if (outputStream != null) {
outputStream.close();
}
}
uploadForm.setMessage("The file " + formFile.getFileName() + " is uploaded successfully.");
return mapping.findForward(SUCCESS);
}
由于文件未在此处接收,因此显示 FileNotFound 异常。 如果我使用带有文本框的普通浏览按钮,代码运行良好。但要求是,文本框应该被隐藏。并且按钮也不应该与 css 混淆。任何想法出了什么问题。
【问题讨论】:
-
不要用js,效果不好。关闭 onclick 并在隐藏文件输入和无操作按钮周围包裹一个
-
用
visibility:hidden代替display:none,这样就可以访问文件名了。 -
@dandavis 我没听懂你。默认浏览按钮和文本字段不显示。你说把这个按钮包裹在
label中,然后让标签打开文件资源管理器? -
@SureshPonnukalai 如果我使用
visibility:hidden,即使它被隐藏了,它不会仍然占用那么多空间吗? -
隐藏文件上传,使用不透明度和大小。然后将标签与 for 属性设置为文件上传的 id。在标签中放置一个没有任何事件的按钮。您现在可以单击虚拟按钮,它将打开文件对话框。
标签: javascript html css spring file-upload