自定义文件上传按钮
可以提供从本地计算机选择文件的选项进一步上传。 但我想要一个自定义按钮,可以打开
文件浏览器对话框
&也可以通过单击自定义按钮进一步上传。请参阅下面的示例。让我们假设
<input type="file" id="file-upload" /> /* file upload */
<input type="button" id="customized" /> /* simple button */
以下是 jQuery fn &它使按钮能够用作上传文件按钮。
$(function(){
$('#customized').click(function() { /* cutomized button clicked */
$('#file-upload').click(); /* Now file upload button auto clicked & file browser dialog opens. */
});
});
上面的 js
在 Windows 机器上工作,而 它在 Linux 上不起作用,为什么?
我也知道操作系统与此无关,而浏览器对此负责。请帮我解决这个问题Windows
- - > mozila
IE
chrome
:工作,请检查示例
Linux
无法在任何浏览器上运行
<input type="file"/>
can give an option to select the file from local machine & upload further.
But i wanted a customized button which enables to open the file browser dialog
& further upload by clicking on customized button too. please see below the example. Let us assume
<input type="file" id="file-upload" /> /* file upload */
<input type="button" id="customized" /> /* simple button */
following is the jQuery fn & it enables the button to works as upload file button.
$(function(){
$('#customized').click(function() { /* cutomized button clicked */
$('#file-upload').click(); /* Now file upload button auto clicked & file browser dialog opens. */
});
});
above js
works on Windows machine while it doesn't work on Linux, Why?
also i know OS has nothing to do and Browser is responsible for this. Please help me to sort out this problemWindows
- - > mozila
IE
chrome
: works, please check example
Linux
not working on any browser
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您不需要显示文件名的栏,则效果很好:) http://jsfiddle.net/fxfPL/
If you don't need the bar which shows file name, this works fine:) http://jsfiddle.net/fxfPL/
这更多地与浏览器版本有关,而不是与操作系统或浏览器品牌有关。
我相信这在 Windows 上的 Opera 中也会失败 - 因为某些浏览器会阻止在文件类型输入上触发事件。
你可以使用这个旧的 CSS 技巧 - http://www.quirksmode.org/dom/inputfile。 html
This is more to do with the browser version rather than the OS or Browser make.
I believe this would also fail in Opera on Windows - as some browsers prevent the firing of events on a file-type input.
You could do this old CSS trick instead - http://www.quirksmode.org/dom/inputfile.html
最近也遇到了同样的问题,看到了这个页面。 Tomas,您的回复非常好,并且在 IE8 上进行了细微调整后效果非常好。
IE8 上的问题是原始浏览按钮的浏览文本与使用按钮替换创建的新浏览文本重叠。对于遇到同样问题的人,您可以将原始上传按钮的不透明度更改为0:
您可以使用另一个按钮来调用文件上传按钮的单击事件。原始文件上传按钮将保持隐藏。 我在原始文件上传按钮中重复使用了 Tomas 的类名来隐藏它。
我相信,如果您使用服务器控件,这个解决方案也会很棒(但请注意,您需要调整服务器控件的单击事件)。
http://jsfiddle.net/shiego926/X98yp/
希望这段代码会有所帮助!
Having the same issue as well recently and saw this page. Tomas your reply is great and worked very well with minor adjustment on IE8.
The issue on IE8 is the browse text of the original browse button overlaps with the new one created using the button replacement. For those experiencing the same issue, you could change the opacity of the original upload button to 0:
You could use another button that will call the click event of the file upload button. The original file upload button will remain hidden. I reused the class name of Tomas' for the original file upload button to hide it.
I believe this solution would also be great if you are using server controls (but note that you need to tweak the binding of the click event of the server control).
http://jsfiddle.net/shiego926/X98yp/
Hope this code will help!