我是个新手自己的写的图片上传类.有什么不对请大家帮忙指出
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>");
}

}
}

































































































































































































































































