输入文件字段到输入文本字段
我什至不知道这是否可行,但有没有一种方法可以将输入文件字段中所选文件的值转移到输入文本字段?
像这样:
I don't even know if this is possible or not but is there a method you can take the value of the selected file in a input file field to a input text field?
Like this:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
挂钩文件字段的
change
事件。Jsfiddle 演示。请注意,IE6/7 错误地给出了完整路径,而其他浏览器正确地仅给出了文件名。
Hook on the
change
event of the file field.Jsfiddle demo. Note that IE6/7 incorrectly gives the full path while other browsers correctly gives only the filename.
这应该可以通过创建一个新的文本输入元素并使用文件输入的
.value
属性填充它来实现。但请注意,出于安全原因,所有现代浏览器仅在
value
属性中存储文件名。您将无法获取所选文件的完整路径。来源: IE8 上的 MSDN
This should be possible by creating a new text input element and populating it with the
.value
property of the file input.Note, however, that all modern browsers store only the file name in the
value
property for security reasons. You will not be able to get the full path of the selected file.Sources: MSDN on IE8
如果不先将文件存储在您自己的服务器上,您就无法做到这一点。
文件输入控件不包含文件的数据。您的浏览器将其作为占位符提供给您,直到您通过 POST 表单提交来提交文件数据。
如果你在寻找文件的路径,你也不能这样做(在现代浏览器中,正如佩卡所说)。浏览器不会将该信息提供给客户端脚本。但是,它可能会提供文件名。
You can't do it without first storing the file on your own server.
The file input control does not contain the file's data. Your browser provides it to you as a placeholder until you submit the file data via a POST form submission.
If you're after the file's path, you also cannot do that (in modern browsers, as Pekka says). The browser does not give that information to client-side scripts. It may provide the filename, however.