您现在的位置是:网站首页> 编程资料编程资料

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

2023-05-24 368人已围观

简介 ASP.NET插件uploadify批量上传文件完整使用教程_实用技巧_

uploadify批量上传文件完整使用教程,供大家参考,具体内容如下

1.首先准备uploadify的js文件,网上一搜一大堆

2.上传页面UpFilePage.aspx

关键代码:

上传文件
添加文件
<%=GetFile() %>

后台的GetFile()方法:

 ///  /// 获取cookie附件信息 ///  ///  protected string GetFile() { #region 获取cookie附件信息 StringBuilder strHtml = new StringBuilder(); HttpCookie fileCookie = 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(@"
"); strHtml.Append(@"
"); strHtml.Append(""); //strHtml.Append(@""); strHtml.Append(@"
"); strHtml.Append(@"" + HttpUtility.UrlDecode(file[0]) + " - 100%
"); strHtml.Append(@"
"); strHtml.Append(@"
"); strHtml.Append(@"
"); strHtml.Append(@"
"); } } } return strHtml.ToString(); #endregion }

3.UploadAction文件夹下的一般处理程序:

 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("\\", "/"); 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(@"
"); strHtml.Append(@"
"); strHtml.Append(""); //strHtml.Append(@""); strHtml.Append(@"
"); strHtml.Append(@"" + HttpUtility.UrlDecode(file[0]) + " - 100%
"); strHtml.Append(@"
"); strHtml.Append(@"
"); strHtml.Append(@"
"); strHtml.Append(@"
"); } } 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(@"
"); strHtml.Append(@"
"); strHtml.Append(""); //strHtml.Append(@""); strHtml.Append(@"
"); strHtml.Append(@"" + HttpUtility.UrlDecode(file[0]) + " - 100%
"); strHtml.Append(@"
"); strHtml.Append(@"
"); strHtml.Append(@"
"); strHtml.Append(@"
"); } } } 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("/") + 1); //文件保存目录路径 String savePath = "/upfiles/"; //文件保存目录URL String saveUrl = "/upfiles/"; //if (context.Request.Cookies["Member"] != null) //{ // savePath += context.Request.Cookies["Member"]["MemberId"] + "/"; // saveUrl += context.Request.Cookies["Member"]["MemberId"] + "/"; //} string Member = Guid.NewGuid().ToString().Trim().Replace("-", ""); savePath += Member + "/"; saveUrl += Member + "/"; //定义允许上传的文件扩展名 /*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 (imgF
                
                

-六神源码网