显示用户想要在网站上上传的图像文件的预览

发布于 2024-10-07 11:09:56 字数 103 浏览 0 评论 0原文

我如何显示用户选择在网站上上传的图像的预览。

我的意思是,当用户从系统中选择文件时,网页已经向他们显示他们想要上传的文件。我如何使用 HTML 和 jQuery 来做到这一点?

How i can show a preview of the image which the user chooses to upload on website.

I mean, when a user chooses the file from their system, have the web-page already show them the files they want to upload. how I can do this using HTML and jQuery?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

通知家属抬走 2024-10-14 11:09:57

你不能,至少在所有浏览器中不能。

这是因为某些浏览器提供了 的完整路径 - 这样您就可以尝试在用户硬盘上显示该元素(请注意某些浏览器的安全设置)

其他浏览器只提供一个文件名 - 您在这里没有任何可显示的内容。

您始终可以做的就是使用 jQuery(.ajax,因此页面不会刷新)上传文件,然后将其显示给用户。在当今时代,上传文件并不那么令人不安(特别是如果您在工作时提供动画)。

如果您确实需要此功能,您始终可以使用 Java Applet(例如 facebook 或 imageshack)来进行预览。不过,并非每个用户都安装并启用了 Java(足够新的版本)。

如果您仍然想为提供完整路径的浏览器实现此功能(例如,您在公司环境中工作并且每个人都使用相同的浏览器,或者您让它优雅地降级并仅向其工作的用户提供该功能) ,您只需使用 .val() 即可获取文件名的路径。在前面添加 file:// 并显示(转储到 img 左右)

更多信息:

You can't, atleast not in all browsers.

This is because some browsers provide the entire path to the <input type='file'> - this way you can try and show the element on the users harddisk (do watch out for certain browsers' security settings)

Other browsers just provide a filename - you've got nothing to show here.

What you can always do, is upload the file using jQuery (.ajax, so the page does not get refreshed) and then show it to the user already. In these modern times, uploading a file is not that disturbing (especially if you provide an animation while it's working).

If you really need this feature, you can always use a Java Applet (think facebook or imageshack) to allow for previews. Not every user has (a recent enough version of) Java installed and enabled though.

If you still want to implement this for browsers that do provide the full path (say, you work in a corporate environment and everyone uses the same browser, or you have it degrade gracefully and just offer the feature to the users for whom it works), you just use the .val() of the <input type='file'> to get the path to the filename. Add a file:// before and display (dump in an img or so)

More info:

徒留西风 2024-10-14 11:09:57
<div class="upload-preview">
  <img />
</div>
<input class="file" name="logo" type="file">



$(document).ready(function(){
var preview = $(".upload-preview img");

$(".file").change(function(event){
   var input = $(event.currentTarget);
   var file = input[0].files[0];
   var reader = new FileReader();
   reader.onload = function(e){
       image_base64 = e.target.result;
       preview.attr("src", image_base64);
   };
   reader.readAsDataURL(file);
  });
});

你可以用这个小提琴
http://jsfiddle.net/Kulgar/57F67/

<div class="upload-preview">
  <img />
</div>
<input class="file" name="logo" type="file">



$(document).ready(function(){
var preview = $(".upload-preview img");

$(".file").change(function(event){
   var input = $(event.currentTarget);
   var file = input[0].files[0];
   var reader = new FileReader();
   reader.onload = function(e){
       image_base64 = e.target.result;
       preview.attr("src", image_base64);
   };
   reader.readAsDataURL(file);
  });
});

you can use this fiddle
http://jsfiddle.net/Kulgar/57F67/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文