Button to show choose a file to upload dialog box

不问归期 提交于 2019-12-03 07:54:38

I think most browsers have this locked down for security purposes. Buttons and text boxes can be manipulated via JavaScript. File input boxes cannot, and for good reason; imagine if a javascript could open a dialog, set the path to a sensitive file on your system, then simulate the button click to download the file!


By the way, if you are looking to style it, perhaps this would work: http://www.quirksmode.org/dom/inputfile.html

<input type="file" style="display:none;" id="inputfile"/>
<a href="javascript:document.getElementById('inputfile').click(); ">try this</a>

Try this one. I think it is useful.. :)

Check this fiddle: http://jsfiddle.net/A4BS7/1/

NOTE:

a) This may not work well on older browsers (mainly IE) that don't fire the change event on the file input.

b) For the upload to work as expected, you'll need to include the <input type="file"> element in your form. The text element can be used for displaying the selected file at best.

It is not possible to alter an input[type=file] as you like, it is a purely native form element.

Besides you won't be able to get the path to the file for security reasons. Old IE versions shows the path but it is not the case anymore with newer versions and you won't be able to do anything with the path on server-side anyway.

There are though some methods to style:

Have a look at plupload, I've used it many times to handle file uploading.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!