图片上传类

 我是个新手自己的写的图片上传类.有什么不对请大家帮忙指出
using  System;
using  System.Web;
using  System.IO;
/// <summary>
/// Upfiles 的摘要说明
/// </summary>

namespace  IceUpFiles
{
    
public class UpFiles
    
{
        
private string path = null;
        
private string fileType = null;
        
private int sizes = 0;
        
private int status = 0;
        
private int maxWith = 120;
        
public UpFiles()
        
{
            
///<summary>
            
/// TODO: 在此处添加构造函数逻辑
            
///</summary>

            path = @"UpFiles"//上传路径
            fileType = "jpg|gif|bmp";
            sizes 
= 200//传文件的大小,默认200KB
            status = 0;//1是生成小图片
            maxWith = 120;//默认生成小图片最大的长度
        }

        
/// <summary>
        
/// 设置上传路径,如:uploadimages
        
/// 

        public string Path
        
{
            
set
            
{
                path 
= @""+value+@"";
            }

        }

        
///<summary>
        
/// 设置上传文件的类型,如:jpg|gif|bmp
        
///</summary>

        public string FileType
        
{
            
set
            
{
                fileType 
= value.ToLower();
            }

        }

        
///<summary>
        
/// 设置上传文件大小,单位为KB 
        
///</summary> 

        public int Sizes
        
{
            
set
            
{
                sizes 
= value * 1024;
            }

        }

        
///<summary>
        
/// 设置上传小图片最大宽度依准 
        
///</summary>

        public int MaxWith
        
{
            
set
            
{
                maxWith 
= value;
            }

        }

        
///<summary>
        
/// 设置是否生成小图片值为1生成小图片返回图片名为(小图|大图) 
        
///</summary>

        public int Status
        
{
            
set
            
{
                status 
= value;
            }

        }

        
///<summary>
        
/// 上传图片
        
/// 上传控件名称
        
/// true则以当前时间创建文件夹,false则为设置的文件夹
        
/// 返回上传图片的相对路径
        
/// </summary>

        public string fileSaveAs(System.Web.UI.HtmlControls.HtmlInputFile Filename, bool creatDirectory)
        
{
            
try
            
{
                
string filePath = null;
                
//以当前时间修改图片的名字或创建文件夹的名字
                string NewFileName = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString();
                
//获得站点的物理路径
                string uploadFilePath = null;
                
if (creatDirectory)
                
{
                    path 
= path + DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + @"";
                    uploadFilePath 
= System.Web.HttpContext.Current.Server.MapPath("."+ path;
                }

                
else
                
{
                    uploadFilePath 
= System.Web.HttpContext.Current.Server.MapPath("."+ path;
                }

                
//获得文件的上传的路径
                string sourceFilePath = Filename.Value.Trim();
                
if (sourceFilePath == "" || sourceFilePath == null)
                
{
                    message(
"您没有上传数据呀,是不是搞错了呀!");
                    
return null;
                }

                
//获得文件扩展名
                string sEx = System.IO.Path.GetExtension(Filename.PostedFile.FileName).Replace(".","");
                
//获得上传文件的大小
                long postFileSize = Filename.PostedFile.ContentLength;
                
//分解允许上传文件的格式
                string[] temp = fileType.Split('|');
                
//设置上传的文件是否是允许的格式
                bool flag = false;
                
//判断上传文件大小
                if (postFileSize >= sizes)
                
{

                    message(
"上传的图片不能大于" + sizes + "KB");
                    
return null;
                }

                
foreach(string data in temp)
                
{
                    
if (data == sEx)
                    
{
                        flag 
= true;
                        
break;
                    }

                }

                
if (!flag)
                
{
                    message(
"目前本系统支持的格式为:" + fileType);
                    
return null;
                }

                System.IO.DirectoryInfo dir 
= new System.IO.DirectoryInfo(uploadFilePath);
                
if (!dir.Exists)
                
{
                    dir.Create();
                }

                
               
                    filePath 
= uploadFilePath + NewFileName + "." + sEx;
                    Filename.PostedFile.SaveAs(filePath);
                    filePath 
= path + NewFileName + "." + sEx;
                
                
if (status == 1)
                
{
                    System.Net.FileWebRequest q 
= (System.Net.FileWebRequest)System.Net.FileWebRequest.Create(uploadFilePath + NewFileName + "." + sEx);
                    System.Net.FileWebResponse p 
= (System.Net.FileWebResponse)q.GetResponse();
                    
int picmaxlong = maxWith;
                    System.Drawing.Image bmp 
= System.Drawing.Image.FromStream(p.GetResponseStream());
                    
int newh;
                    
float f;
                    
if (bmp.Width > bmp.Height)
                    
{
                        
if (bmp.Width != bmp.Height)
                        
{
                            f 
= (float)picmaxlong / (float)bmp.Width;
                            newh 
= (int)(f * (float)bmp.Height);
                        }

                        
else
                        
{
                            newh 
= maxWith;
                            picmaxlong 
= maxWith;
                        }


                    }

                    
else
                    
{
                        
if (bmp.Width != bmp.Height)
                        
{
                            f 
= (float)maxWith / (float)bmp.Height;
                            newh 
= (int)(f * (float)bmp.Width);
                            
int newwh = newh;
                            newh 
= picmaxlong;
                            picmaxlong 
= newwh;
                        }

                        
else
                        
{
                            newh 
= maxWith;
                            picmaxlong 
= maxWith;
                        }


                    }

                    System.Drawing.Image.GetThumbnailImageAbort callb 
= null;
                    System.Drawing.Image image 
= System.Drawing.Image.FromFile(uploadFilePath + NewFileName + "." + sEx);
                    System.Drawing.Image newimage 
= image.GetThumbnailImage(picmaxlong, newh, callb, new System.IntPtr());
                    
//把缩略图保存到指定的虚拟路径
                    newimage.Save(uploadFilePath +"small_" + NewFileName + "." + sEx);
                    
//释放image对象占用的资源
                    image.Dispose();
                    
//释放newimage对象的资源
                    newimage.Dispose();
                    filePath 
= path + "small_" + NewFileName + "." + sEx + "|" + filePath;
                }

                
return filePath.Replace("/","/");
            }

            
catch
            
{
                message(
"出现未知错误");
                
return null;
            }

        }

        
private void message(string msg, string url)
        
{
            System.Web.HttpContext.Current.Response.Write(
"<script language='javascript'> alert('" + msg + "');window.location='" + url + "'; </script>");
        }


        
private void message(string msg)
        
{
            System.Web.HttpContext.Current.Response.Write(
"<script language='javascript'> alert('" + msg + "'); </script>");
        }


    }

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值