多图片上传

今天做了多图片上传的功能,觉得有一些东西还是需要保存起来的,特此写在这里让大家一起来分享一下,有什么好的建议好的提议听给我留言奥,

下面是前台控制多图片的方法:

ContractedBlock.gif ExpandedBlockStart.gif 代码
 
   
1
2
3 < script language ="javascript" >
4 // 添加图片,用div隐藏的方式
5 var i = 1 ;
6 function addImg(){
7 if (i < 5 )
8 {
9 i ++ ;
10 $( " #div_img_ " + i).css({display: " block " })
11 } else {
12 alert( " 最多只能上传5张图片 " );
13 }
14 }
15
16 // js控制图片类型 并且用滤镜显示图片实现 上传图片预览功能
17 function $(o){ return document.getElementById(o);}
18 function CheckImgCss(o,img)
19 {
20 if ( ! / \.((jpg)|(bmp)|(gif)|(png))$ / ig.test(o.value))
21 {
22 alert( ' 只能上传jpg,bmp,gif,png格式图片! ' );
23 o.outerHTML = o.outerHTML;
24 }
25 else
26 {
27 $(img).filters.item( " DXImageTransform.Microsoft.AlphaImageLoader " ).src = o.value;
28 // $('Image1').src = o.value;//这里IE7已经不支持了。所以才有上面的方法。     
29 //这个代码应该写在页面中,是用滤镜预览图片的功能 <div id="img" style="filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=<%= pic%>,sizingMethod=scale);width:102px;height:100px;"></div>
30 }
31 }
32 </ script >

下面是后台代码:

ContractedBlock.gif ExpandedBlockStart.gif 代码
 
   

protected void Button1_Click( object sender, EventArgs e)
{
Random r
= new Random();
// 这样循环,可以同时上传多个文件。前台已经有文件格式的判断,有错误提示了。这里只要过滤掉非法文件即可,无需提示了。

HttpFileCollection file
= Request.Files;
for ( int i = 0 ; i < file.Count; i ++ )
{
if (Request.Files[i].ContentLength > 0 )
{
string ex = System.IO.Path.GetExtension(Request.Files[i].FileName).ToLower();
if ( " .jpg.gif.png.bmp " .Contains(ex))
{
string newFileName = DateTime.Now.ToString( " yyyyMMddHHmmss " ) + r.Next( 100 , 999 ).ToString() + ex;
// 保存文件名到数据库
// xxxxxxxxxxxxxxxx
// xxxxxxxxxxxxxxxx
string str = file[ " fuAdd5 " ].FileName;
file[i].SaveAs(Server.MapPath(
" ~/files/ " + newFileName));
pic
= newFileName;
}
}
}
}

下面是我写的一个处理缩略图的方法,如果可能拿来就可以使用的一个方法,很方便:

ContractedBlock.gif ExpandedBlockStart.gif 代码
 
   

/// <summary>
/// 原图画出缩略图 并保存到服务器返回图片路径
/// </summary>
/// <param name="hfc"> 图片的集合 HttpFileCollection </param>
/// <param name="index"> 要保存的图片的坐标 </param>
/// <param name="savePath"> 要保存的路径以及名称 </param>
/// <returns></returns>
public bool SaveImg(HttpFileCollection hfc, int index, string savePath)
{
if (hfc[index].ContentLength <= 0 )
return false ;
System.Drawing.Image o
= System.Drawing.Image.FromStream(hfc[index].InputStream);
int tWidth = 170 ; // 设置缩略图初始宽度 大于170自动取170
int tHeight = 110 ; // 设置缩略图初始高度 大于110自动取110
// 按比例计算出缩略图的宽度和高度
if (o.Width >= o.Height)
{
tHeight
= ( int )Math.Floor(Convert.ToDouble(o.Height) * (Convert.ToDouble(tWidth) / Convert.ToDouble(o.Width)));
}
else
{
tWidth
= ( int )Math.Floor(Convert.ToDouble(o.Width) * (Convert.ToDouble(tHeight) / Convert.ToDouble(o.Height)));
}
Bitmap bImg
= new Bitmap(tWidth, tHeight);
Graphics g
= Graphics.FromImage(bImg);
g.InterpolationMode
= System.Drawing.Drawing2D.InterpolationMode.High; // 设置高质量插值法
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; // 设置高质量,低速度呈现平滑程度
g.Clear(Color.Transparent); // 清空画布并以透明背景色填充
g.DrawImage(o, new Rectangle( 0 , 0 , tWidth, tHeight), new Rectangle( 0 , 0 , o.Width, o.Height), GraphicsUnit.Pixel);
// 命名保存
// 命名保存 路径:目录+时间戳+随机数+后缀名
// string savePath = "/files/s" + DateTime.Now.ToString("yyyyMMddHHmmss") + new Random().Next(100, 999) + System.IO.Path.GetExtension(hfc[index].FileName);
try
{
bImg.Save(Server.MapPath(
" ~ " ) + savePath);
}
catch (Exception ex)
{
throw ex;
}
finally
{
o.Dispose();
bImg.Dispose();
g.Dispose();
}
return true ;
}

希望小弟写的这点东西对大家能有些帮助呵呵。。

转载于:https://www.cnblogs.com/bq-blog/archive/2010/07/17/1779622.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值