<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">   <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server">     <title>无标题页</title>     <style type="text/css">           #preview           {               margin: 2px 0 0 0;           }           #previewImage{ border:1px solid #ddd; padding:3px;}     </style>     <link rel="stylesheet" type="text/css" href="jQueryPlugin/Uploadify/uploadify.css" /> <script src="js/jquery.min.js" type="text/javascript"></script> <script type="text/javascript" src="js/DrawImage.js"></script> <script src="jQueryPlugin/Uploadify/jquery.uploadify-3.1.min.js" type="text/javascript"></script>  <script type="text/javascript">      $(function () {          $("#uploadImage").uploadify({              width: 100,              height: 26,              swf: 'jQueryPlugin/Uploadify/uploadify.swf', //[*]swf的路径              uploader: 'HttpHandler/Uploadify.ashx', //[*]一般处理程序              cancelImg: 'jQueryPlugin/Uploadify/uploadify-cancel.png', //取消图片路径              multi: false,              'fileTypeDesc': '图片文件',              fileTypeExts: '*.gif;*.jpg;*.jpeg;*.png', //允许上传的文件类型,使用分号(”;)”分割 例如:*.jpg;*.gif,默认为null(所有文件类型)              'fileSizeLimit': '6000KB',              onUploadSuccess: function (file, data, response) {//上传完成时触发(每个文件触发一次)                  if (data.indexOf('错误提示') > -1) {                      alert(data);                  }                  else {                      $("#previewImage").attr("src", data.substr(2)).hide().fadeIn(2000);                  }              },              'onUploadError': function (file, errorCode, errorMsg, errorString) {//当单个文件上传出错时触发                  alert('文件:' + file.name + ' 上传失败: ' + errorString);              },              buttonText: '选择图片'          });      });     </script> </head> <body> <form id="form1" runat="server">      <div id="upload">          <input type="file" name="uploadImage" id="uploadImage" />      </div>      <div id="preview">          <img id="previewImage" src="images/no_pic.gif" alt="暂无图片" onload="DrawImage(this,120,120,'')" />      </div> </form> </body> </html>


Uploadify.ashx文件代码:


using System; using System.Collections.Generic; using System.Linq; using System.Web;   using System.IO;   namespace CCMS.Web.HttpHandler {     /// <summary>     /// Uploadify 的摘要说明     /// </summary>     public class Uploadify : IHttpHandler     {         protected string AllowExt = "7z|aiff|asf|avi|bmp|csv|doc|docx|fla|flv|gif|gz|gzip|jpeg|jpg|mid|mov|mp3|mp4|mpc|mpeg|mpg|ods|odt|pdf|png|ppt|pptx|pxd|qt|ram|rar|rm|rmi|rmvb|rtf|sdc|sitd|swf|sxc|sxw|tar|tgz|tif|tiff|txt|vsd|wav|wma|wmv|xls|xlsx|xml|zip";//支持的文件格式         int FileMaxSize = 10240;//文件大小,单位为K         public void ProcessRequest(HttpContext context)         {             context.Response.ContentType = "text/plain";             //context.Response.Write("Hello World");                //HttpPostedFile file = context.Request.Files[0];//获取上传文件方式一             HttpPostedFile fileUpload = context.Request.Files["Filedata"];//获取上传文件方式二             if (fileUpload != null)             {                 try                 {                     string UploadDir = "~/upload/Uploadify/";//图片保存的文件夹                     //图片保存的文件夹路径                     string path = context.Server.MapPath(UploadDir);                     //每天上传的图片一个文件夹                     string folder = DateTime.Now.ToString("yyyyMM");                     //如果文件夹不存在,则创建                     if (!Directory.Exists(path + folder))                     {                         Directory.CreateDirectory(path + folder);                     }                     //上传图片的扩展名                     string fileExtension = fileUpload.FileName.Substring(fileUpload.FileName.LastIndexOf('.'));                     //判断文件格式                     if (!CheckValidExt(fileExtension))                     {                         context.Response.Write("错误提示:文件格式不正确!" + fileExtension);                         return;                     }                     //判断文件大小                     if (fileUpload.ContentLength > FileMaxSize * 1024)                     {                         context.Response.Write("错误提示:上传的文件(" + fileUpload.FileName + ")超过最大限制:" + FileMaxSize + "KB");                         return;                     }                     //保存图片的文件名                     //string saveName = Guid.NewGuid().ToString() + fileExtension;                     //使用时间+随机数重命名文件                     string strDateTime = DateTime.Now.ToString("yyMMddhhmmssfff");//取得时间字符串                     Random ran = new Random();                     string strRan = Convert.ToString(ran.Next(100, 999));//生成三位随机数                     string saveName = strDateTime + strRan + fileExtension;                       //保存图片                     fileUpload.SaveAs(path + folder + "/" + saveName);                     context.Response.Write(UploadDir + folder + "/" + saveName);                 }                 catch                 {                     context.Response.Write("错误提示:上传失败");                 }             }         }           public bool IsReusable         {             get             {                 return false;             }         }           #region 检测扩展名的有效性 bool CheckValidExt(string sExt)           /// <summary>         /// 检测扩展名的有效性         /// </summary>         /// <param name="sExt">文件名扩展名</param>         /// <returns>如果扩展名有效,返回true,否则返回false.</returns>         public bool CheckValidExt(string strExt)         {             bool flag = false;             string[] arrExt = AllowExt.Split('|');             foreach (string filetype in arrExt)             {                 if (filetype.ToLower() == strExt.ToLower().Replace(".", ""))                 {                     flag = true;                     break;                 }             }             return flag;         }         #endregion       } }