本地上传图片的n种方法

兼容性好,功能强大的是webupload,请详细查看http://fex.baidu.com/webuploader/getting-started.html

falsh图像上传插件:http://fullavatareditor.com/

方法一:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="X-UA-Compatible" content="IE=7" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <script src="uploadPreview.js" type="text/javascript"></script>


    <style type="text/css">
    .img_upload{max-width:500px;height:180px;background:#FFC;margin:30px auto;clear:both;}
#imgdiv{float:left;width:100px;height:100px;border:1px solid #ccc;}
    a{display:inline-block; width:70px; height:35px;position:relative;text-align:center;color:#898484;font-size:15px;line-height:40px;text-decoration:none;float:left;margin-left:20px;margin-top:20px;background:red url(images/bdsc.png) no-repeat 0px 0px;}
    #up_img{position:absolute;top:0;left:0;width:70px;height:35px;opacity:0; filter:alpha(opacity=0);}
</style>
</head>
<body>
<div class="img_upload">
   <div id="imgdiv">
      <img id="imgShow" width="100" height="100" />
   </div>
   <a href="javascript:void(0);"><input id="up_img" type="file" /></a>
</div>
<script>
window.onload = function() {
    new uploadPreview({ UpBtn: "up_img", DivShow: "imgdiv", ImgShow: "imgShow" });
}
</script>
</body>

</html>

方法二:

<div class="add_item">
        <div class="fileInput">
             <input type="file" name="upfile1" class="upfile" id="upfile1" onchange="previewImage(this,'upfileResult1','imghead1')">
              <input class="upFileBtn" type="button" value="" name="other_certificate" value="上传图片" onclick="$('#upfile1').click()">
         </div>
         <div class="pay_sfz" id="upfileResult1"></div>
 </div>


function previewImage(file,preview,imghead) {
    var MAXWIDTH = 318;
    var MAXHEIGHT = 200;
    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 {
        var sFilter='filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale,src="';
        file.select();
        var src = '';
        if(navigator.userAgent.indexOf("MSIE 9.0")>0){
            document.getElementById('picDiv').focus();
            src = document.selection.createRange().text;
        }else{
            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:"+MAXWIDTH+"px;height:"+MAXHEIGHT+"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 src="../js/imageFile.js"></script>

<div class="upVideo">
                         <div class="upLeft">视频缩略图</div>
                         <div class="upRight">
                            <div class="btn1">
                                <span>本地上传</span>
                                <input type="file" id="fileVideo" name="fileVideo" value="" onchange=""/>
                            </div>
                            <span class="spanTips">*上传图片不得超过2M大小</span>
                             <input type="text" value="" class="addrImg" name="addrImg" style="display:none;" />
                            <img class="imgShow" src="">
                         </div>
  </div>
<input type="button" value="上传视频" id="upBtn">

<script>

$(function () {//上传图片
    var showimg = $('.imgShow');//放置上传的图片
    var spanTips = $(".spanTips");//图片上传出现的提示
    $("#fileVideo").wrap("<form id='myupload' action='/stu/upload' method='post' enctype='multipart/form-data'></form>");
    $("#fileVideo").change(function(){
$('.imgShow').css("display","block");//点击上传图片时,放置上传图片的位置才显示
        $("#myupload").ajaxSubmit({
            url:'/stu/upload',
            dataType:  'json',
            beforeSend: function() {
                $(function() {//图片上传格式及大小验证
                    $('#fileVideo').checkFileTypeAndSize({
                        allowedExtensions: ['jpg','png','gif'],
                        maxSize: 2000,
                        success: function() {
                            spanTips.html('上传中...')
                        },
                        extensionerror: function() {
                            spanTips.html('允许的格式为:jpg、png、gif');
                            return false;
                        },
                        sizeerror: function() {
                            spanTips.html('图片最大 不能超过2MB');
                            return false;
                        }
                    });
                });
            },
            success: function(data) {
                if(data.status=="success") {
                    var img = "/upload/student/"+data.uid+"/"+data.img;
                    showimg.attr("src", img);//把上传的图片的路径赋值给showimg
                    $('.addrImg').attr("value", data.img);//把图片的名称放到addrImg的value中
                    spanTips.html("上传成功");
                }else{
                    alert(data.message);
                }
            },
            error:function(xhr){
                spanTips.html("上传失败,格式应为:jpg、png、gif且图片不大于2MB");
            }
        });
    });
$("#upBtn").click(function(){//上传视频表单提交
 $("#upForm").submit();
});
});

</script>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值