public class uploadBug : IHttpHandler
{
private HttpContext context;
public void ProcessRequest(HttpContext context)
{
String aspxUrl = context.Request.Path.Substring(0, context.Request.Path.LastIndexOf("/") + 1);
//文件保存目录路径
String savePath = "../Files/File/BulletinAttached/";
//文件保存目录URL
String saveUrl = aspxUrl + "../Files/File/BulletinAttached/";
//定义允许上传的文件扩展名
Hashtable extTable = new Hashtable();
extTable.Add("image", "gif,jpg,jpeg,png,bmp");
extTable.Add("flash", "swf,flv");
extTable.Add("media", "swf,flv,mp3,wav,wma,wmv,mid,avi,mpg,asf,rm,rmvb");
extTable.Add("file", "doc,docx,xls,xlsx,ppt,htm,html,txt,zip,rar,gz,bz2");
//最大文件大小
int maxSize = 1000000;
this.context = context;
HttpPostedFile imgFile = context.Request.Files["imgFile"];
if (imgFile == null)
{
showError("请选择文件。");
}
String dirPath = context.Server.MapPath(savePath);
if (!Directory.Exists(dirPath))
{
showError("上传目录不存在。");
}
String dirName = context.Request.QueryString["dir"];
if (String.IsNullOrEmpty(dirName))
{
dirName = "image";
}
if (!extTable.ContainsKey(dirName))
{
showError("目录名不正确。");
}
String fileName = imgFile.FileName;
String fileExt = Path.GetExtension(fileName).ToLower();
if (imgFile.InputStream == null || imgFile.InputStream.Length > maxSize)
{
showError("上传文件大小超过限制。");
}
if (String.IsNullOrEmpty(fileExt) || Array.IndexOf(((String)extTable[dirName]).Split(','), fileExt.Substring(1).ToLower()) == -1)
{
showError("上传文件扩展名是不允许的扩展名。\n只允许" + ((String)extTable[dirName]) + "格式。");
}
//创建文件夹
dirPath += dirName + "/";
saveUrl += dirName + "/";
if (!Directory.Exists(dirPath))
{
Directory.CreateDirectory(dirPath);
}
String ymd = DateTime.Now.ToString("yyyyMMdd", DateTimeFormatInfo.InvariantInfo);
dirPath += ymd + "/";
saveUrl += ymd + "/";
if (!Directory.Exists(dirPath))
{
Directory.CreateDirectory(dirPath);
}
String newFileName = DateTime.Now.ToString("yyyyMMddHHmmss_ffff", DateTimeFormatInfo.InvariantInfo) + fileExt;
String filePath = dirPath + newFileName;
ImageFormat mf;
if (fileExt.ToLower().Equals(".gif"))
mf = System.Drawing.Imaging.ImageFormat.Gif;
else if (fileExt.ToLower().Equals(".png"))
mf = System.Drawing.Imaging.ImageFormat.Png;
else if (fileExt.ToLower().Equals(".bmp"))
mf = System.Drawing.Imaging.ImageFormat.Bmp;
else
mf = System.Drawing.Imaging.ImageFormat.Jpeg;
//imgFile.SaveAs(filePath);
MemoryStream iamge = BuildImageByFixedSize(imgFile.InputStream, 620, 350, mf);
// 620 350 是想压缩后最大宽度和高度 可根据自己的要求修改
Image inputImage = Image.FromStream(iamge);
inputImage.Save(string.Format("{0}", filePath), mf);
String fileUrl = saveUrl + newFileName;
Hashtable hash = new Hashtable();
hash["error"] = 0;
hash["url"] = fileUrl;
context.Response.AddHeader("Content-Type", "text/html; charset=UTF-8");
context.Response.Write(JsonMapper.ToJson(hash));
context.Response.End();
}
private void showError(string message)
{
Hashtable hash = new Hashtable();
hash["error"] = 1;
hash["message"] = message;
context.Response.AddHeader("Content-Type", "text/html; charset=UTF-8");
context.Response.Write(JsonMapper.ToJson(hash));
context.Response.End();
}
public bool IsReusable
{
get
{
return true;
}
}
public static MemoryStream BuildImageByFixedSize(Stream imageStream, int targetWidth, int targetHeight, ImageFormat imageFormat)
{
Image image = Image.FromStream(imageStream);
return BuildImageByFixedSize(image, targetWidth, targetHeight, imageFormat);
}
public static MemoryStream BuildImageByFixedSize(Image imageDraw, int targetWidth, int targetHeight, ImageFormat imageFormat)
{
int width = imageDraw.Width, height = imageDraw.Height;
MemoryStream outStream = new MemoryStream();
if (width > targetWidth)
{
targetWidth = 620; targetHeight = height * 620 / width;
}
if (width < targetWidth)
{
if (height > targetHeight) { targetHeight = 350; targetWidth = width * 350 / height; }
else { targetWidth = width; targetHeight = height; }
}
//if (targetWidth == 0 && targetHeight == 0) { targetWidth = width; targetHeight = height; }
//if (targetWidth > width && targetHeight > height) { targetWidth = width; targetHeight = height; }
//if (targetWidth > width && targetHeight < height) { targetWidth = width; }
//if (targetWidth < width && targetHeight > height) { targetHeight = height; }
//if (targetWidth < width && targetHeight > height) { targetHeight = height; }
using (Bitmap bitmap = new Bitmap(imageDraw, targetWidth, targetHeight))
{
using (Graphics graphics = Graphics.FromImage(bitmap))
{
graphics.Clear(Color.Transparent);
graphics.DrawImage(imageDraw, new Rectangle(0, 0, targetWidth, targetHeight));
imageDraw.Dispose();
bitmap.Save(outStream, imageFormat);
return outStream;
}
}
}
}