<html>
<head>
<title>a test for selection object</title>
<script type="text/javascript" src="js/jquery-1.7.2.js"></script>
<script type='text/javascript'>
$(function(){
$("#addImage").click(function() {
if($("ul#files li").size() < 6){
$("<li><input type='file' accept='image/bmp,image/png,image/gif,image/jpeg' value='' name='upload' onchange='previewImage(this)'> (图片格式:*.bmp,*.png,*.gif,*.jpeg)<input type='button' value='删除' onclick='removeItem(this)'/><div class='preview'></div></li>").appendTo("#files");
} else {
alert("添加图片数量不能超过6个");
}
});
});
function removeItem(obj) {
$(obj).parent("li").remove();
};
function previewImage(file)
{
var MAXWIDTH = 200;
var MAXHEIGHT = 200;
var div = file.parentNode.childNodes[3];
if (file.files && file.files[0])
{
div.innerHTML = '<img class=imghead>';
var img = div.getElementsByTagName('img')[0];
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 = document.selection.createRange().text;
var i = 0;
div.innerHTML = "<img id=imghead>";
i = i + 1;
//div.innerHTML = '<img class=imghead>';
var img = div.getElementsByTagName('img')[0];
img.filters.item('DXImageTransform.Microsoft.AlphaImageLoader').src = src;
//TODO临时解决
var rect = {top:44, left:0, width:200, height:113};
status =('rect:'+rect.top+','+rect.left+','+rect.width+','+rect.height);
div.innerHTML = "<div class='divhead' style='width:"+rect.width+"px;height:"+rect.height+"px;margin-top:"+rect.top+"px;margin-left:"+rect.left+"px;"+sFilter+src+"\"'></div>";
//var rect = clacImgZoomParam(MAXWIDTH, MAXHEIGHT, img.offsetWidth, img.offsetHeight);
//status =('rect:'+rect.top+','+rect.left+','+rect.width+','+rect.height);
//div.innerHTML = "<div class='divhead' "+200+"px;height:"+200+"px;margin-top:"+200+"px;margin-left:"+200+"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>
</head>
<body>
<form>
<ul id="files">
<li><input type="file" onchange="previewImage(this)" class="input-file" id="ss" accept="image/bmp,image/png,image/gif,image/jpeg" value="" name="upload"> (图片格式:*.bmp,*.png,*.gif,*.jpeg)<label class="error"></label><div class="preview" id="preview"></div></li>
</ul>
<input id="addImage" type="button" value="继续添加" />
</form>
</body>
</html>
备注:
IE下会有问题
分享到:
相关推荐
上传图片本地预览效果,选择图片就可在页面上看到
图片上传本地预览插件
这个html文件用html5实现了本地图片上传,在上传服务器前回显预览的功能
此工程下页面test.jsp为图片选中预览功能实现
选择图片时出现本地预览,具有等比例缩放、文件类型判断等功能。兼容ie以及火狐、chorme等浏览器。对于7以上高版本火狐也同样支持
使用jquery上传前,预览图片,裁剪,示例使用php接收上传的文件,并且保存为裁剪后的图片。不需要上传后再裁剪图片,只需要本地裁剪好即可,裁剪的时候也可以旋转图片。
主要介绍了jQuery实现的上传图片本地预览效果,结合实例形式分析了jQuery上传图片本地预览所涉及的相关页面元素属性动态操作实现技巧,需要的朋友可以参考下
Jquery 表单验证+本地图片上传-切割-预览 完全项目 希望不要用于商业用途
html5图片上传本地生成缩略图预览
图片上传,html页面,本地预览,后台前台页面代码都有
html5移动端图片上传本地生成缩略图预览,单图上传
Html5上传图片前生成本地预览,可以在本地预览图片,再上传到服务器
该资源为php上传视频并且预览的代码,
写在前面:计划实现图片上传预览功能,但是本地图片上传所获得路径为 C:\fakepath\”+文件名的形式,得不到文件真实路径,所以无法直接预览,于是采用将图片上传至服务器,传回服务器路径,实现预览。前端采用通过...
在网上找到一个高手写本地图片上传预览的插件,测试通过ie6,ff2.0、3.6,做成个Demo拿来和大家分享,jsp实现
1 上传图片前实现本地预览图片。 2 兼容ie,firefox 3 实现判断文件大小。火狐下实现了上传前判断图片大小,ie下目前是采用ActiveXObject方法,但会弹框提示,不友好,提供ajax方式后台判断文件大小。 4 判断图片类型...
主要介绍了JS+HTML5实现上传图片预览效果,结合完整实例形式分析了javascript上传图片本地预览的具体操作步骤与相关实现技巧,需要的朋友可以参考下
插件描述:js上传图片本地预览。 参考实例:http://www.jq22.com/jquery-info6180
图片上传预览是一种在图片上传之前对图片进行本地预览的技术。 使用户选择图片后能立即查看图片,而不需上传服务器,提高用户体验。 但随着浏览器安全性的提高,要实现图片上传预览也越来越困难。 不过群众的智慧是...