ASP.NET插件uploadify批量上传文件完整使用教程(2)

public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; string operate = context.Request["operate"]; if (operate == "deleteFile") { #region 删除文件附件信息 //获取文件路径 string filePath = context.Server.MapPath(context.Request["file"]); //判断文件是否存在 if (File.Exists(filePath)) File.Delete(filePath);//删除文件 //获取附件cookie信息 HttpCookie fileCookie = context.Request.Cookies["FileCookie"]; string[] fileArray = new string[1]; if (fileCookie != null) { filePath = filePath.Remove(0, filePath.IndexOf("upfiles")).Replace("\\", "https://www.jb51.net/"); if (fileCookie.Value.Contains("|")) fileArray = fileCookie.Value.Split('|'); else fileArray[0] = fileCookie.Value; string strFile = ""; for (int i = 0; i < fileArray.Length; i++) { if (!fileArray[i].Contains(filePath)) strFile += fileArray[i] + "|"; } if (strFile.Contains("|")) strFile = strFile.Remove(strFile.Length - 1); fileCookie.Value = strFile; fileCookie.Expires = DateTime.Now.AddDays(1); fileCookie.HttpOnly = true; context.Response.AppendCookie(fileCookie); StringBuilder strHtml = new StringBuilder(); if (fileCookie.Value.Contains("|")) fileArray = fileCookie.Value.Split('|'); else fileArray[0] = fileCookie.Value; foreach (string objFile in fileArray) { if (!string.IsNullOrEmpty(objFile) && objFile.Contains(",")) { string[] file = objFile.Split(','); strHtml.Append(@"<div>"); strHtml.Append(@"<div>"); strHtml.Append("<a href='javascript:deleteFile(\"" + file[1] + "\")'></a>"); //strHtml.Append(@"<img src='https://www.jb51.net/Scripts/jquery.uploadify-v2.1.0/cancel.png'>"); strHtml.Append(@"</div>"); strHtml.Append(@"<span>" + HttpUtility.UrlDecode(file[0]) + "</span><span> - 100%</span><div>"); strHtml.Append(@"<div>"); strHtml.Append(@"</div>"); strHtml.Append(@"</div>"); strHtml.Append(@"</div>"); } } context.Response.Write(strHtml.ToString()); } #endregion } else if (operate == "GetFile") { #region 获取上传的附件并展示 StringBuilder strHtml = new StringBuilder(); HttpCookie fileCookie = context.Request.Cookies["FileCookie"]; if (fileCookie != null) { string[] fileArray = new string[1]; if (fileCookie.Value.Contains("|")) fileArray = fileCookie.Value.Split('|'); else fileArray[0] = fileCookie.Value; foreach (string objFile in fileArray) { if (!string.IsNullOrEmpty(objFile) && objFile.Contains(",")) { string[] file = objFile.Split(','); strHtml.Append(@"<div>"); strHtml.Append(@"<div>"); strHtml.Append("<a href='javascript:deleteFile(\"" + file[1] + "\")'>"); //strHtml.Append(@"<img src='https://www.jb51.net/Scripts/jquery.uploadify-v2.1.0/cancel.png'></a>"); strHtml.Append(@"</div>"); strHtml.Append(@"<span>" + HttpUtility.UrlDecode(file[0]) + "</span><span> - 100%</span><div>"); strHtml.Append(@"<div>"); strHtml.Append(@"</div>"); strHtml.Append(@"</div>"); strHtml.Append(@"</div>"); } } } context.Response.Write(strHtml.ToString()); #endregion } }

4.上传文件uploadHandler.ashx一般处理程序代码,文件上传路径可以根据剧情需要自由设定:

public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; HttpCookie fileCookie = context.Request.Cookies["FileCookie"]; if (fileCookie != null) { string[] fileArray = new string[1]; if (fileCookie.Value.Contains("|")) fileArray = fileCookie.Value.Split('|'); if (fileArray.Length >= 5) return; } else { fileCookie = new HttpCookie("FileCookie"); fileCookie.Value = ""; context.Response.Cookies.Add(fileCookie); } String aspxUrl = context.Request.Path.Substring(0, context.Request.Path.LastIndexOf("https://www.jb51.net/") + 1); //文件保存目录路径 String savePath = "/upfiles/"; //文件保存目录URL String saveUrl = "/upfiles/"; //if (context.Request.Cookies["Member"] != null) //{ // savePath += context.Request.Cookies["Member"]["MemberId"] + "https://www.jb51.net/"; // saveUrl += context.Request.Cookies["Member"]["MemberId"] + "https://www.jb51.net/"; //} string Member = Guid.NewGuid().ToString().Trim().Replace("-", ""); savePath += Member + "https://www.jb51.net/"; saveUrl += Member + "https://www.jb51.net/"; //定义允许上传的文件扩展名 /*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,mp4"); extTable.Add("file", "doc,docx,xls,xlsx,ppt,htm,html,txt,zip,rar,gz,bz2,swf,flv,mp3,wav,wma,wmv,mid,avi,mpg,asf,rm,rmvb,mp4,wps");*/ //最大文件大小 int maxSize = 5242880; HttpPostedFile imgFile = context.Request.Files["imgFile"]; /*if (imgFile == null) { showError("请选择文件。"); }*/ String dirPath = context.Server.MapPath(savePath); if (!Directory.Exists(dirPath)) { Directory.CreateDirectory(dirPath); //showError("上传目录不存在。"); } String dirName = context.Request.QueryString["dir"]; if (String.IsNullOrEmpty(dirName)) { dirName = "file"; } /*if (!extTable.ContainsKey(dirName)) { showError("目录名不正确。"); }*/ String fileName = imgFile.FileName; String fileExt = Path.GetExtension(fileName).ToLower(); /*if (String.IsNullOrEmpty(fileExt) || Array.IndexOf(((String)extTable[dirName]).Split(','), fileExt.Substring(1).ToLower()) == -1) { showError("上传文件扩展名是不允许的扩展名。\n只允许" + ((String)extTable[dirName]) + "格式。"); } if (dirName.Contains("image")) { if (imgFile.InputStream == null || imgFile.InputStream.Length > maxSize) { showError("上传文件超过5M大小限制。"); } }*/ //创建文件夹 dirPath += dirName + "https://www.jb51.net/"; saveUrl += dirName + "https://www.jb51.net/"; if (!Directory.Exists(dirPath)) { Directory.CreateDirectory(dirPath); } String ymd = DateTime.Now.ToString("yyyyMMdd", DateTimeFormatInfo.InvariantInfo); dirPath += ymd + "https://www.jb51.net/"; saveUrl += ymd + "https://www.jb51.net/"; if (!Directory.Exists(dirPath)) { Directory.CreateDirectory(dirPath); } String newFileName = DateTime.Now.ToString("yyyyMMddHHmmss_ffff", DateTimeFormatInfo.InvariantInfo) + fileExt; String filePath = dirPath + newFileName; imgFile.SaveAs(filePath); 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();*/ if (fileCookie != null) { string strFile = fileCookie.Value; if (!string.IsNullOrEmpty(strFile)) strFile = strFile + "|" + HttpUtility.UrlEncode(fileName) + "," + fileUrl; else strFile = HttpUtility.UrlEncode(fileName) + "," + fileUrl; fileCookie.Value = strFile; fileCookie.Expires = DateTime.Now.AddDays(1); fileCookie.HttpOnly = true; context.Response.AppendCookie(fileCookie); } context.Response.Write("1"); context.Response.End(); }

5.所有代码敲完OK,可以收获成果了:

ASP.NET插件uploadify批量上传文件完整使用教程

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wjyfjs.html