【问题标题】:How to open file chooser from JSP page on click of a link?单击链接时如何从 JSP 页面打开文件选择器?
【发布时间】:2014-08-07 14:13:25
【问题描述】:

我正在开发一个允许用户上传图像文件的 JSP 页面。该文件稍后将保存在数据库中,并将用作他们的个人资料图像。如何在单击 href 链接时打开文件选择器对话框?

我知道<input type = "file" /> 解决方案,但这不是页面设计的一部分,我必须仅从href 打开文件选择器。

【问题讨论】:

  • 可以通过javascript完成,没有直接的方法

标签: javascript html jsp


【解决方案1】:

试试这个,

<input type="file" id="upload" name="upload" style="visibility: hidden; width: 1px; height: 1px" multiple />
<a href="" onclick="document.getElementById('upload').click(); return false">Upload File</a>

查看工作中的FIDDLE

也可以使用CSS效果,

Read this link获取信息

【讨论】:

    【解决方案2】:

    在单击 href 按钮时尝试打开文件选择器,

    <html>
    <head>
    <script>
    function openDialog()
    {
    document.getElementById("file1").click();
    }
    </script>
    </head>
    <body>
    <input type="file" id="file1" style="display:none">
    <a href="#" onclick="openDialog();return;">open Dialog</a>
    </body></html>
    

    【讨论】:

      【解决方案3】:

      HTML

        <a href="#">
          Your Anchor tag
          </a>
      
      
          <input type="file" id="file"  />
      

      jQuery

      ​​>
      $("a").trigger("click");
      
      $(document).on("click", "a", function(){
          $('#file').click();
      });
      

      JSFIDDLE DEMO

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-04-04
        • 2011-12-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多