js 显示刚刚上传的图片 (onchange事件)

     <table>
            <tr width="100">
                <td>上传商场图片:</td>
                <td>
                    <input type="file" name="img" onchange="previewImage(this)">
                </td>
            </tr>
            <tr height=124px;>
                    <td ></td>
                    <td >
                        <inline id="preview">
                            <img id="imghead" style="width:186px;height:124px;"
                                src=‘/image/{$mall_id}/{$activity.img}‘>
                        </inline>
                    </td>
                </tr>
        </table>   
<script>
    // 上传图片时显示图片 js
	function previewImage(file)

        {

          var MAXWIDTH  = 260;

          var MAXHEIGHT = 180;

          var div = document.getElementById(‘preview‘);

          if (file.files && file.files[0])

          {

              div.innerHTML =‘<img id=imghead>‘;

              var img = document.getElementById(‘imghead‘);

              img.onload = function(){

                var rect = clacImgZoomParam(MAXWIDTH, MAXHEIGHT, img.offsetWidth, img.offsetHeight);

                img.width  =  rect.width;

                img.height =  rect.height;

//                 img.style.marginLeft = rect.left+‘px‘;

                img.style.marginTop = rect.top+‘px‘;

              }

              var reader = new FileReader();

              reader.onload = function(evt){img.src = evt.target.result;}

              reader.readAsDataURL(file.files[0]);

          }

          else //兼容IE

          {

            var sFilter=‘filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale,src="‘;

            file.select();

            var src = document.selection.createRange().text;

            div.innerHTML = ‘<img id=imghead>‘;

            var img = document.getElementById(‘imghead‘);

            img.filters.item(‘DXImageTransform.Microsoft.AlphaImageLoader‘).src = src;

            var rect = clacImgZoomParam(MAXWIDTH, MAXHEIGHT, img.offsetWidth, img.offsetHeight);

            status =(‘rect:‘+rect.top+‘,‘+rect.left+‘,‘+rect.width+‘,‘+rect.height);

            div.innerHTML = "<div id=divhead style=‘width:"+rect.width+"px;height:"+rect.height+"px;margin-top:"+rect.top+"px;"+sFilter+src+"\"‘></div>";

          }

        }

        function clacImgZoomParam( maxWidth, maxHeight, width, height ){

            var param = {top:0, left:0, width:width, height:height};

            if( width>maxWidth || height>maxHeight )

            {

                rateWidth = width / maxWidth;

                rateHeight = height / maxHeight;

                if( rateWidth > rateHeight )

                {

                    param.width =  maxWidth;

                    param.height = Math.round(height / rateWidth);

                }else

                {

                    param.width = Math.round(width / rateHeight);

                    param.height = maxHeight;

                }

            }

            param.left = Math.round((maxWidth - param.width) / 2);

            param.top = Math.round((maxHeight - param.height) / 2);

            return param;

        }
</script>

  

时间: 2024-07-31 08:05:24

js 显示刚刚上传的图片 (onchange事件)的相关文章

MVC应用程序显示上传的图片

前两篇<MVC应用程序实现上传文件>http://www.cnblogs.com/insus/p/3590907.html和<MVC应用程序实现上传文件(续)>http://www.cnblogs.com/insus/p/3594834.html,我们练习了上传文件,当然上传图片也是一样. 此篇我们练习,怎样在MVC应用程序中显示用户上传的图片.为了接近更真实的练习,Insus.NET决定对以前的程序修改一下,就是上传的目录把原来的Temp目录改为UploadFiles目录.也就是

如何预览将要上传的图片-使用H5的FileAPI

这篇将要说的东西已经不新鲜了. 参考资料: Reading files in JavaScript using the File APIs (鉴于作者在美国, 我姑且认为作者母语是英语, 当然链接中有本地化可以选择中文) 要做什么效果呢, 就是页面上有个<input type="file" />, 用户选择需要上传的图片后, 页面上显示将要上传的图片. 以前呢, 需要Ajax将原图上传到服务器, 得到成功响应后在页面上添加一张图片. 如果用户发现上传错了, 需要把服务器上的

js图片上传验证图片格式和大小尺寸

<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title></title> <script type="text/javascript"> function getImageSize(obj){ var _file=document.getElementById("file

一款简单实用的上传文件图片插件并且兼容移动端zyupload.js

1.下载zyupload插件包 包含的文件如下图: 2.在/images/fileType文件夹下定义上传文件的显示图标 如下图所示: 3.打开zyupload.js,修改上传后显示文件图标路径 如下图所示: 4.调用的JS代码 <link rel="stylesheet" href="~/Content/assets/css/zyupload-1.0.0.min.css"> <script type="text/javascript&q

MVC应用程序显示上传的图片(续)

上一篇<MVC应用程序显示上传的图片>http://www.cnblogs.com/insus/p/3597543.html 最后有提及没有实现用户点击图片,显示原图的功能.此篇Insus.NET来完成它. 想使用一个dialog box来呈现原图.这个jQuery的UI已经有了,引用相关的js类库即可. 代到HomeController.cs控制器,添加一个ContentResult方法: 即是传入图片名称,找到上传目录的相对应的图片.打开ViewUploadPhoto.cshtml 视图,

magento 1.9 上传后图片前后台无法正常显示

1.上传后图片不显示,设置 允许 flash 2.保证php 执行是内存大小至少为为128M,多种方式设置,这里以init_set为例子,在index.php 加入下面一行代码,根据情况而定 ini_set('memory_limit','128M'); 原文地址:https://www.cnblogs.com/pa200318/p/10241850.html

调起摄像头、上传下载图片、本地展示图片

之前那偏微信JS-SDK授权的文章实现了分享接口,那么这里总结一下如何在微信里面通过js调起原生摄像头,以及上传下载图片. 1.配置 页面引入通过jssdk授权后,传入wx对象,首先配置需要的接口 wx.config({ /* debug: true, */ appId: appid, timestamp: timestamp, nonceStr: nonceStr, signature: signature, jsApiList: [ 'chooseImage',//拍照或从手机相册中选图接口

微信开发之调起摄像头、本地展示图片、上传下载图片

之前那篇微信JS-SDK授权的文章实现了分享接口,那么这里总结一下如何在微信里面通过js调起原生摄像头,以及上传下载图片. 1.配置 页面引入通过jssdk授权后,传入wx对象,首先配置需要的接口 wx.config({ /* debug: true, */ appId: appid, timestamp: timestamp, nonceStr: nonceStr, signature: signature, jsApiList: [ 'chooseImage',//拍照或从手机相册中选图接口

CKeditor七牛云JS SDK前端上传插件修改

七牛云官方有放出JS SDK,没有我想使用的CKeditor前端上传插件,所以结合七牛官方的Javascript SDK对CKeditor做了一些修改使它能够直接上传到七牛云,又同时保留了上传到本地服务的接口. 优点和缺点1.在前端上传到七牛云,不消耗服务器带宽和流量.空间.2.保留了CKeditor上传到自己服务器的能力.3.支持拖拽和剪切板黏贴图片上传(因为是保存为png格式,建议只黏贴色彩单调的图片,要不然图片会很大,浪费流量).4.拖拽和剪切板黏贴图片.不支持4M以上的文件,因为没有分块