获取拖放到字段上的资源的 URL
我有一个带有特定输入字段的 html 页面,我想添加以下功能。
用户应该能够将资源拖放到字段上。此操作的结果应该是该资源的 url 出现在该字段中。
该资源可以是本地文件,导致类似于 file:///home/me/document
或 file://c:\windows-files\document.doc
的 url代码>.
该资源也可以是 Web 资源,导致类似于 http://host.nl/document.doc
甚至 ftp://host.nl/document.doc
的 url code>,尽管此时我对 ftp 资源并不真正感兴趣。我假设从网络浏览器的地址栏执行网页 url 的 dnd 操作,或者从桌面等客户端计算机上执行文件的 dnd 操作。
与网络应用程序一样,我希望此功能能够在大多数平台上运行。 (Linux/Win/MacOS、Firefox/Chrome/Safari/IE/Opera)
范例是 html 和 JavaScript。
I have an html page with a certain input field and I want to add the following functionality.
The user should be able to drag and drop a resource onto the field. The result of this action should be that the url of the resource appears in the field.
The resource could be a local file, resulting in a url like file:///home/me/document
or file://c:\windows-files\document.doc
.
The resource could also be a web resource, resulting in a url like http://host.nl/document.doc
or even ftp://host.nl/document.doc
, although at this point I'm not really interested in ftp resources. I'm assuming a dnd-action of a web-page url from the address bar of a web browser, or a dnd-action of a file on the client machine from e.g. the desktop.
As usual for web apps, I want this functionality to work on most platforms. (Linux/Win/MacOS, Firefox/Chrome/Safari/IE/Opera)
The paradigm is html and JavaScript.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 Firefox 中,您可以使用 file.mozFullPath。
但是,此变量仅在 Firefox 中出现,在 Chrome 或 Safari 中不起作用。
In Firefox you can use file.mozFullPath.
However, this variable presents only in Firefox and don't work in Chrome or Safari.
根据所有现代浏览器采取的安全措施,不可能获得拖放到浏览器中的文件的真实完整路径。
现在,所有主流浏览器都将文件路径替换为“/fakepath/'FileName'”,其中“FileName”是您选择的文件的名称。
但是,您仍然可以执行拖放功能并仅获取拖到浏览器中的文件的文件名。
评论中指出的相关问题的答案进行修改的 jsfiddle
这是对问题http://jsfiddle.net 的 /c7cqN/
Do to security measures put in place by all modern browsers it is impossible to get the real full path of a file which has been drag and dropped into a browser.
All major browsers now replace the file path with "/fakepath/'FileName'" where 'FileName' is the name of the file you selected.
You can however still do drag and drop functionality and get JUST the file name of the file you dragged into the browser.
Here is a jsfiddle of a modification of the answer to the related question noted in the comments of the question
http://jsfiddle.net/c7cqN/
我已经完成了 Keith Abramo 的代码,易于阅读并添加了视图更改,
从其他浏览器或选项卡拖放 URL 可用于创建书签之类的东西..
我发现它很有用!
i have done code of Keith Abramo simple to read and added view changes
Drag and dropp URL from other Browser or Tab could be used for create something like Bookmarks..
i find it useful!