<script type="text/javascript">
function onUploadImgChange(sender){
if( !sender.value.match( /.jpg|.gif|.png|.bmp/i ) ){
alert('图片格式无效!');
return false;
}
var objPreview = document.getElementById( 'preview' );
var objPreviewFake = document.getElementById( 'preview_fake' );
var objPreviewSizeFake = document.getElementById( 'preview_size_fake' );
if( sender.files && sender.files[0] ){
objPreview.style.display = 'block';
objPreview.style.width = 'auto';
objPreview.style.height = 'auto';
// Firefox 因安全性问题已无法直接通过 input[file].value 获取完整的文件路径
objPreview.src = sender.files[0].getAsDataURL();
}else if( objPreviewFake.filters ){
// IE7,IE8 在设置本地图片地址为 img.src 时出现莫名其妙的后果
//(相同环境有时能显示,有时不显示),因此只能用滤镜来解决
// IE7, IE8因安全性问题已无法直接通过 input[file].value 获取完整的文件路径
sender.select();
var imgSrc = document.selection.createRange().text;
objPreviewFake.filters.item(
'DXImageTransform.Microsoft.AlphaImageLoader').src = imgSrc;
objPreviewSizeFake.filters.item(
'DXImageTransform.Microsoft.AlphaImageLoader').src = imgSrc;
autoSizePreview( objPreviewFake,
objPreviewSizeFake.offsetWidth, objPreviewSizeFake.offsetHeight );
objPreview.style.display = 'none';
}
}
function onPreviewLoad(sender){
autoSizePreview( sender, sender.offsetWidth, sender.offsetHeight );
}
function autoSizePreview( objPre, originalWidth, originalHeight ){
var zoomParam = clacImgZoomParam( 150,150, originalWidth, originalHeight );
objPre.style.width = zoomParam.width + 'px';
objPre.style.height = zoomParam.height + 'px';
objPre.style.marginTop = zoomParam.top + 'px';
objPre.style.marginLeft = zoomParam.left + 'px';
}
function clacImgZoomParam( maxWidth, maxHeight, width, height ){
var param = { width:width, height:height, top:0, left:0 };
if( width>maxWidth || height>maxHeight ){
rateWidth = width / maxWidth;
rateHeight = height / maxHeight;
if( rateWidth > rateHeight ){
param.width = maxWidth;
param.height = height / rateWidth;
}else{
param.width = width / rateHeight;
param.height = maxHeight;
}
}
param.left = (maxWidth - param.width) / 2;
param.top = (maxHeight - param.height) / 2;
return param;
}
</script>
</head>
<body>
<div id="preview_wrapper">
<div id="preview_fake">
<img id="preview" src="/pxcoa/images/nophoto.jpg"/>
</div>
</div>
<br/>
<input id="upload_img" type="file" οnchange="onUploadImgChange(this)"/>
<br/>
<img id="preview_size_fake" >
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="zh-CN" dir="ltr"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> <link href="http://www.iteye.com/stylesheets/attachment.css?1344923975" media="screen" rel="stylesheet" type="text/css" /> <script src="http://www.iteye.com/javascripts/application.js?1338174647" type="text/javascript"></script> </head> <body> <form action="/attachments" enctype="multipart/form-data" id="attachment_form" method="post"><div style="margin:0;padding:0;display:inline"><input name="authenticity_token" type="hidden" value="3qGC4UswCKlkK2LP2eywMROPpi1szkeN51sFaSAM8y4=" /></div> <input id="attachment_uploaded_data" name="attachment[uploaded_data]" οnchange="upload(this.value);" size="30" type="file" /> <span id="spinner" style="display:none;">上传中 <img alt="Spinner" src="http://www.iteye.com/images/spinner.gif?1324994303" /></span> </form> <script type="text/javascript"> if(document.location.host != "www.iteye.com") document.domain = "iteye.com"; function upload(fileName) { if (!$A(['jpg','jpeg','bmp','png','gif','rar','zip', 'tar', 'gz', 'jar', 'war', 'bz2', '7z', 'pdf', 'swf']).any(function(extName){return new RegExp('\\.'+extName+'$','i').test(fileName);})){ alert("如果您上传图片,请上传png, jpg, gif或者bmp格式的图片\n如果您上传附件,请先压缩再上传"); return false; } var counter = parseInt(window.parent.$('attachments_counter').value) + 1; window.parent.$('attachments_counter').value = counter; window.parent.$('submit_button').value = counter + " 个文件正在上传中,请等待完成后再提交"; $('spinner').show(); var iframes = window.parent.$("attachment_iframes"); iframes.appendChild(iframes.getElementsByTagName('iframe')[0].cloneNode(false)); $('attachment_form').submit(); } </script> </body> </html>
上传新的文件:
<div id="attachment_iframes">
<iframe src="/attachments/new" style="border:0px;height:30px;width:100%;" frameborder="0" border="0" cellspacing="0" allowTransparency="true" scrolling="no" resizable="no"></iframe>
<input type="hidden" value="0" class="max-value-0" id="attachments_counter"/>
</div>