【问题标题】:Hiding textbox of the file Uploading browse button隐藏文件的文本框上传浏览按钮
【发布时间】: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


【解决方案1】:

不要使用display:none,使用height:0;width:0;opacity:0;

为什么?

当您使用display:none 时,该元素会从文档结构中完全删除,因此您的代码中不会感知

请注意,显示“无”不会创建不可见的框; 它根本没有创建任何盒子。 http://www.w3.org/TR/CSS2/visuren.html#propdef-display

【讨论】:

  • @smashIT,元素在 style 修改后是可见的,还是在更改后不可见且仍未被检测到?
猜你喜欢
  • 1970-01-01
  • 2015-09-20
  • 2017-06-09
  • 1970-01-01
  • 2018-01-08
  • 1970-01-01
  • 2012-06-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多