引用到的js ----ajaxfileupload.js
页面
imageFile.ashx文件
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.IO;
using System.Drawing;
namespace AjaxLibrary
{
public class imageFile:IHttpHandler
{
bool IHttpHandler.IsReusable
{
get { return false; }
}
void IHttpHandler.ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/html";//设置输出的类型
***
string param=context.Request.QueryString["param"];//类型
string filenames = context.Request.QueryString["filename"];
string error = "";
string msg = "";
string filePath = "";
switch (param)
{
case "uploadfile":
bool boolen = false;
HttpPostedFile postFile = context.Request.Files
[filenames];//httpPostFile
if (postFile.ContentLength == 0)
{
error = "无效的文件";
}
else
{
string mapPathDir="~/upload_files/";
if(System.IO.Directory.Exists
(context.Server.MapPath(mapPathDir))!=true)
{
System.IO.Directory.CreateDirectory
(context.Server.MapPath(mapPathDir));
}
UploadFile(context, postFile,
mapPathDir,boolen,out error,out msg,out filePath);
}
string result =
"{error:'"+error+"',msg:'"+msg+"',filepath:'"+filePath+"'}";
context.Response.Output.Write(result);
break;
case "delfile":
string RequestFilePath = context.Request.QueryString
["filepath"];//获取要删除的图片的路径() 如:upload_files/XXXXYYY.gif
DelImgFile(context, RequestFilePath, out error, out
msg);
string delresult = "{ error:'" + error + "', msg:'" +
msg + "'}";
context.Response.Output.Write(delresult);
break;
}
}
void UploadFile(HttpContext context,HttpPostedFile
httppostFile,string paths,bool boolBenl,out string err,out string
msg,out string filepath)
{
string fileName =Path.GetExtension
(httppostFile.FileName).ToLower();
string firstName = DateTime.Now.ToString("yyyyMMddHHmmss");
string ThumbName = "Thumb_" + DateTime.Now.ToString
("yyyyMMddHHmmss");
string newNamePath =paths+firstName + fileName;
string ThumbNamePath =context.Server.MapPath(paths +
ThumbName + fileName);
string mapPathStr = context.Server.MapPath(newNamePath);
httppostFile.SaveAs(mapPathStr);
if (boolBenl == true)
{
CoverDoubleImage(httppostFile,202,200,ThumbNamePath);
}
err = "";
msg = "上传成功";
filepath = firstName + fileName;
}
//删除目录文件
void DelImgFile(HttpContext context, string imgName,out string
error,out string msg)
{
if (imgName.Length != 0 && imgName.IndexOf('/')>-1)
{
string paths = context.Server.MapPath(imgName);
string Thumbpaths = context.Server.MapPath
(imgName.Split('/')[0] + "/Thumb_" + imgName.Split('/')[1]);
try
{
if (System.IO.File.Exists(paths) == true)
File.Delete(paths);
if (System.IO.File.Exists(Thumbpaths) == true)
File.Delete(Thumbpaths);
}
catch (Exception ee) { error = ee.Message; }
error = "";
msg = "删除成功";
}
else
{
error = "";
msg = "无效的请求";
}
}
#region 上传图片并自动生成缩略图JPG
///
/// 上传图片并自动生成缩略图JPG
///
///
FileUpload控件引用
///
缩略图宽度
///
缩略图高度
///
缩略图路径
public static void CoverDoubleImage(HttpPostedFile FileUpload1,
int tWidth, int tHeight, string savePath)
{
//检查上传文件的
是否有效
if (FileUpload1.ContentType.ToLower().IndexOf("image") < 0)
{
// Use.JS.Alert("图片格式错误");
return;
}
//生成原图
System.IO.Stream oStream = FileUpload1.InputStream;
System.Drawing.Image oImage = System.Drawing.Image.FromStream
(oStream);
int oWidth = oImage.Width; //原图宽度
int oHeight = oImage.Height; //原图高度
//生成缩略原图
Bitmap tImage = new Bitmap(tWidth, tHeight);
Graphics g = Graphics.FromImage(tImage);
g.InterpolationMode =
System.Drawing.Drawing2D.InterpolationMode.High; //设置高质量插值法
g.SmoothingMode =
System.Drawing.Drawing2D.SmoothingMode.HighQuality;//设置高质量,低速度
呈现平滑程度
g.Clear(Color.Transparent); //清空画布并以透明背景色填充
g.DrawImage(oImage, new Rectangle(0, 0, tWidth, tHeight), new
Rectangle(0, 0, oWidth, oHeight), GraphicsUnit.Pixel);
try
{
//以JPG格式保存图片
//oImage.Save(savePath,
System.Drawing.Imaging.ImageFormat.Jpeg); //保存原图
tImage.Save
(savePath,System.Drawing.Imaging.ImageFormat.Jpeg); //保存缩略图
}
catch (Exception ex)
{
throw ex;
}
finally
{
//释放资源
oImage.Dispose();
g.Dispose();
tImage.Dispose();
}
}
#endregion
}
}