<!DOCTYPE html>
<html lang="en">
<head>
<title> </title>
<script type="text/javascript" src="js/jquery.min.js"></script>
</head>
<body>
请选取一个图像文件: <input type = "file" id = "file" name = "file" />
<div id = "result"> </div>
</body>
</html>
<script type = "text/javascript" >
/*
uploadBtn: string 点击上传的input按钮的id
返回图片通过base64转换后的url
*/
$("#file").on("change",function(e){
var file = e.target.files[0];
if (file) {
var reader = new FileReader();
reader.onabort = function ( event ) {console.log("中断")};
reader.onerror = function ( event ) {console.log("出错")};
reader.onloadstart = function ( event ) {console.log("开始")};
reader.onprogress = function ( event ) {console.log("正在读取")};
reader.onload = function ( event ) {console.log("成功读取")};
reader.onloadend = function ( event ) {console.log("读取完成,无论成功失败")}
reader.onload = function ( event ) {
var txt = event.target.result;
$("#result").html(txt);
<pre class=”brush: java; gutter: true;”>
$("#result").append('<img src="'+txt+'">');
</pre>
$("#result").append('\<\img \src="'+txt+'"\>\');
return event.target.result;
};
}
reader.readAsDataURL( file );
})
</script>
兼容ie的写法 (还没有整理)
function PreviewImg(el,previewEl,opts){
this.$el=$(el);
this.$previewEl=$(previewEl);
//上传图片触发change事件,改变this指向
var that=this;
this.$el.change(function(){
$.proxy(that.preview(this),that)
})
this.opts=$.extend({},PreviewImg.DEFAULTS,opts)
}
//预览图片
PreviewImg.prototype.preview=function(file){
var objthis=this;
var maxWidth =objthis.opts.maxWidth;
var maxHeight = objthis.opts.maxHeight;
var $previewEl = objthis.$previewEl;
$previewEl.html('<img id=preImg>')
var img = $('#preImg').get(0);
if (file.files && file.files[0])
{
img.onload = function(){
var rect = objthis._cutImg(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 = document.selection.createRange().text;
img.filters.item('DXImageTransform.Microsoft.AlphaImageLoader').src = src;
var rect = objthis._cutImg(maxWidth, maxHeight, img.offsetWidth, img.offsetHeight);
$previewEl.html("<div id=divhead style='width:"+rect.width+"px;height:"+rect.height+"px;margin-left:"+rect.left+"px;margin-top:"+rect.top+"px;"+sFilter+src+"\"'></div>");
}
}
//裁剪图片,等比缩放尺寸
PreviewImg.prototype._cutImg=function(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;
}
//默认参数,预览图片的宽高限制
PreviewImg.DEFAULTS={
maxHeight:190,
maxWidth:260
}