jquery图片预览

平时我们在做图片上传的时候,如果可以让用户选择图片的时候,看到图片的效果,那这样用户体验会好很多,因为用户可以就可以决定是否继续用这张图片,尤其是和ajaxuploadfile结合使用的时候,图片的预览让其做的更加的完美。

代码是这样的

$(function() {
    var path,
        clip = $("#img"),    //图片显示的地址   img的id  <img id="#img" src="#" />
        FileReader = window.FileReader;  

    $("#file").change(function() {       //#file是  <input type=‘file‘ id="file" /> 

        if (FileReader) {
            var reader = new FileReader(),
                file = this.files[0];
            reader.onload = function(e) {
                clip.attr("src", e.target.result);
            };
            reader.readAsDataURL(file);
        }
        else {
            path = $(this).val();
            if (/"\w\W"/.test(path)) {
                pathpath = path.slice(1,-1);  

            }
            clip.attr("src",path);

        }
    });
})

其实原理就是读取input type="file" 的路径,将这个路劲赋给 要显示的img的src那里

html代码

<!-- 上传的input  -->
<input type="file" id="file">

<!-- 预览的图片我想在这个img这里显示 -->
<img src="" id="img"  />

  

时间: 2024-08-05 14:49:31

jquery图片预览的相关文章

强大的图片展示插件,JQuery图片预览展示插件

只需要引入JQuery.js , viewer.css 和 viewer.js <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js&qu

jquery图片预览使图片在屏幕正中间。

<ul> <li><img src='image/u1.jpg'></li> <li><img src='image/u2.jpg'></li> <li><img src='image/u3.jpg'></li></ul><div class="bg"> <img class="bgImg" src="&qu

jquery图片预览功能的效果

效果如上: index.html <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Image Preview with jQuery</title> <meta name="description" content="">

jquery实现上传图片及图片大小验证、图片预览效果代码

jquery实现上传图片及图片大小验证.图片预览效果代码 上传图片验证 */ function submit_upload_picture(){     var file = $('file_c').value;     if(!/.(gif|jpg|jpeg|png|gif|jpg|png)$/.test(file)){            alert("图片类型必须是.gif,jpeg,jpg,png中的一种")        }else{      $('both_form')

基于jQuery图文排版图片预览特效

基于jQuery图文排版图片预览特效.这是一款基于jQuery+CSS3实现的鼠标点击图片弹出画廊切换特效. 在线预览   源码下载 实现的代码. html代码: <div id="fullscreen"> <div id="fullscreen-inner"> <div id="fullscreen-inner-left" class="fullscreen-inner-button">&

dwz+jquery+fileupload+springmvc实现文件上传 及图片预览

1 前台jsp:文件的上传利用了iframe实现局部刷新功能.使用了apache的fileupload组件,用到的jar: commons-fileupload.jar,commons-io.jarDiskFileItemFactory fac = new DiskFileItemFactory();ServletFileUpload upload = new ServletFileUpload(fac);upload.setHeaderEncoding("utf-8"); Html代

图片预览

下方点击小图标,上方显示大图标. 布局文件如下 <body> <h1>图片预览</h1> <p> <img id="largeImg" src="images/img1-lg.jpg" alt="Large Image" /> </p> <p class="thumbs"> <!-- 点击小图片 看到大图片 只是超连接的话 会新打开页面,

Uploadify—借助Uploadify插件实现图片预览时如何解决Chrome浏览器报“喔唷,崩溃啦”

今天借助Uploadify插件实现了图片预览及其上传的功能,可是在Chrome浏览器中会时不时地出现如下图所示的情况: 解决方法:使用这种方法引用该插件js文件: <script type="text/javascript"> document.write("<script type='text/javascript' src='<%=basePath %>js/uploadify/jquery.uploadify.min.js?" +

HTML5实现图片预览功能

两种方式实现 URL FileReader Index.jsp文件 <%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">