jQuery - 如何从输入文件 img 制作缩略图
我想用 html 制作一个精美的图像上传系统。当用户选择图像到输入文件中时,将显示图像的缩略图。 正如我所读到的,由于安全问题,无法从输入文件中获取图像的路径。 有人可以告诉我如何用 javascript (使用 jQuery)来实现这个吗? 谢谢你!
I want to make a fancy image uploading system in html. When the user selects an image into a input file, a thumbnail of the image will show up.
As I have read there is no way to get the path of the image from the input file because of the security issues.
Can someone tell me how can I make this with javascript (using jQuery)?
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我可能有一个技巧:
尝试:
I might have a trick for this:
try:
在文件上传到服务器之前,您无法访问文件内容,除非您使用在这方面具有一些奇特功能的 Chrome。
您可以做的是上传文件,然后获取使用 PHP 生成的缩略图的文件,我认为这不是您正在寻找的答案。因此,给你一个公平的答案:不,你不能这样做。
You can't access the file contents before the file is uploaded to your server, unless you're using Chrome which has some fancy features in this area.
What you can do is upload the file and then fetch the file generated as a thumbnail with PHP, I don't think that's the answer you're looking for thought. So instead, to give you a fair answer: no, you can't do this.
我建议使用 jQuery
FileFive
插件:https://github.com/JDBurnZ/jquery-file Five
Internet Explorer 10、Firefox 和 Chrome 全部支持 HTML5 文件对象,允许您读取文件的大小、文件名和 mime 类型,以及文件的实际内容;如果您想显示所选图像的缩略图 无需上传它们。
工作 jsFiddle 示例: http://jsfiddle.net/URqBk/
示例代码:
I'd recommend using the jQuery
FileFive
plug-in:https://github.com/JDBurnZ/jquery-filefive
Internet Explorer 10, Firefox and Chrome all support HTML5 File Objects, which allows you to read the file's size, filename and mime type, as well the actual contents of the file; the later of which is useful if you want to display thumbnails of selected images without ever uploading them.
Working jsFiddle Example: http://jsfiddle.net/URqBk/
Example Code: