js 上传照片预览

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
<style type="text/css">
#preview{width:260px;height:190px;border:1px solid #000;overflow:hidden;}
#imghead {filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=image);}
</style>
<script type="text/javascript">
 //图片上传预览    IE是用了滤镜。
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.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>     
</head>     
<body>


<div id="preview">
    <img id="imghead" border=0 src="head_180.jpg" width="180" height="180" />
</div>
<input type="file" οnchange="previewImage(this)" />
   
</body>     
</html>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在微信小程序中上传文件,你可以使用 `wx.chooseImage` 或 `wx.chooseFile` 方法来上传图片或其他文件。 1. `wx.chooseImage` 方法可以让用户选择图片并上传至服务器,代码示例: ```js wx.chooseImage({ count: 1, // 最多可以选择的图片张数,默认1张 sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有 sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有 success: function (res) { // 返回选定照片的本地文件路径列表,tempFilePaths可作为img标签的src属性显示图片 var tempFilePaths = res.tempFilePaths; wx.uploadFile({ url: '服务器地址', // 上传接口地址 filePath: tempFilePaths[0], // 上传文件路径 name: 'file', // 上传文件名称 success: function (res) { // 上传成功后的处理逻辑 } }) } }) ``` 2. `wx.chooseFile` 方法可以让用户选择文件并上传至服务器,代码示例: ```js wx.chooseFile({ success(res) { // 选择文件后的处理逻辑 wx.uploadFile({ url: '服务器地址', // 上传接口地址 filePath: res.tempFilePaths[0], // 上传文件路径 name: 'file', // 上传文件名称 success: function (res) { // 上传成功后的处理逻辑 } }) } }) ``` 要预览上传的图片或文件,可以使用 `wx.previewImage` 方法来预览图片,代码示例: ```js wx.previewImage({ current: '当前图片的url', // 当前显示图片的链接,不填则默认为urls的第一张 urls: ['图片1的url', '图片2的url'] // 需要预览的图片链接列表 }) ``` 对于其他类型的文件,可以使用 `wx.openDocument` 方法来打开预览,代码示例: ```js wx.openDocument({ filePath: '文件的本地路径', success: function (res) { // 打开文档成功的处理逻辑 } }) ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值