用图像替换普通文件上传输入
这里有一个新手问题。
我有一个表单,其中一个字段用于文件上传。我不想使用旁边带有“选择文件”按钮的无聊旧的普通文本输入框,而是希望有一个图像,当您单击该图像时,会打开对话框来浏览照片。
我希望能够做到这一点的方式是使用两种形式。当用户单击图像时,IE 会出现一个模式框,其中包含表单上传输入。用户选择文件并单击“提交”,用户将返回到表单。
这似乎不起作用,因为我认为在表单中包含表单一定是不好的做法:)有没有办法这样做?
另一种方法是,我可以以某种方式用“选择文件”按钮和我自己的图形替换通常的文本输入框,但尽管谷歌,我还是不知道如何做到这一点。
任何想法
Bit of a newbie question here.
I have a form and one of it's fields is for a file upload. Instead of having the boring old usual text input box with a 'choose file' button beside it, I'd like to have an image which when you click opens the dialog box to browse for the photo.
The way I was hoping to be able to do this was with two forms. IE when the user clicks the image a modal box appears with form upload input in it. User chooses file and clicks submit the user is returned to the form.
That doesn't seem to work because having a form inside a form must be bad practice i suppose :) Is there a way to do it like this?
The alternative is that I can somehow replace the usual text input box with the 'choose file' button with my own graphic but despite google I ain't found out how to do that.
Any ideas
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
非常简单的解决方案 - 只需为您的输入添加一个标签标记
,然后只需将
background-image
属性添加到 #image div :)Very simple solution - simply put a label tag for your input
And the just add a
background-image
property to the #image div :)由于文件输入工作方式存在大量安全问题,因此它们很难修复。然而,真正有效的是这样的方案:
现在,单击您希望它们看起来的样式的元素实际上将被浏览器解释为单击文件输入。您必须对 IE 进行一些调整,因为 IE7 允许用户直接在输入中键入内容,而其他浏览器在单击任何位置时都会立即启动文件选择器。
编辑 - 这里是一个至少可以在 Chrome 中运行的 jsfiddle。 HTML:
它包装了我将使用自己的 CSS 设计样式的“假”文件输入以及真正的
元素。这是 CSS:
外部容器被设置为“position:relative”,以便轻松将真实的
放置在假的东西上。假的东西有我编造的奇特风格,它的大小与真实文件输入的整体大小几乎相同。真品是绝对定位、透明的。
这里有一些 jQuery 来驱动它:
浏览器不会给你完整的路径名,但他们会给你它的一部分。某些浏览器(Chrome 和 IE)会为您提供明显虚假的路径前缀,因此代码会将其删除(因为它没有用)。
Because of the heap of security issues around how file inputs work, they're pretty hard to fix. What does work, however, is a scheme like this:
Now clicks on your elements styled the way you want them to look will actually be interpreted by the browser as clicks on the file input. You have to tweak things somewhat for IE, because IE7 allows the user to type directly into the input while other browsers all immediately launch the file chooser when the element is clicked anywhere.
edit — here is a jsfiddle that works in Chrome at least. The HTML:
That wraps the "fake" file input that I'll style with my own CSS, as well as the real
<input>
element. Here's the CSS:The outer container is made "position: relative" to make it easy to position the real
<input>
over the fake stuff. The fake stuff has my made-up fancy styles, and it's sized so that it's just about the same as the overall size of a real file input. The real one is absolutely positioned and transparent.Here's some jQuery to drive it:
Browsers won't give you the complete pathname, but they'll give you a part of it. Some browsers (Chrome and IE) give you an obviously-fake path prefix, so the code strips that out (because it's useless).
文件上传字段非常有限。请参阅:http://www.quirksmode.org/dom/inputfile.html
File upload fields are quite limited. See: http://www.quirksmode.org/dom/inputfile.html