最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to open file chooser from JSP page on click of a link? - Stack Overflow

programmeradmin3浏览0评论

I am working on a JSP page that allows user to upload an image file. This file will be later saved in database and will be used as their profile image. How can I open a file chooser dialog on click of a href link ?

I know about the <input type = "file" /> solution but that is not part of the page design and I must open the file chooser from a href only.

I am working on a JSP page that allows user to upload an image file. This file will be later saved in database and will be used as their profile image. How can I open a file chooser dialog on click of a href link ?

I know about the <input type = "file" /> solution but that is not part of the page design and I must open the file chooser from a href only.

Share edited Aug 7, 2014 at 5:45 Santhosh 8,2275 gold badges31 silver badges56 bronze badges asked Aug 7, 2014 at 5:29 AmitAmit 13.4k18 gold badges83 silver badges149 bronze badges 1
  • it can be done through javascript , no direct ways – Santhosh Commented Aug 7, 2014 at 5:34
Add a ment  | 

3 Answers 3

Reset to default 2

Try this ,

<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>

See the working FIDDLE

And also can be done using CSS effect ,

Read this link for information

try this for open file chooser on click of href button,

<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>

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

发布评论

评论列表(0)

  1. 暂无评论