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

时间:2021-05-28

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

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

2.上传页面UpFilePage.aspx

关键代码:

<html xmlns="http://plete": function () {$.ajax({type: "post",url: "/UploadAction/UploadHandler.ashx",data: { operate: "GetFile" },async: false,success: function (objdata) {$("#fileSave").html(objdata);fileCount = $("#fileSave>div.uploadifyQueueItem").length;var less = 5 - fileCount;if (less <= 0) {$("#a_upload").attr("href", "javascript:");$("#fileQueue").html("");return false;} else {$("#a_upload").attr("href", "javascript:$('#uploadify').uploadifyUpload()");return true;}}});},"onCancel": function () {fileCount = $("#fileSave>div.uploadifyQueueItem").length;var less = 5 - fileCount;if (less <= 0) {$("#a_upload").attr("href", "javascript:");return false;} else {$("#a_upload").attr("href", "javascript:$('#uploadify').uploadifyUpload()");return true;}},});});function deleteFile(path) {$.ajax({type: "post",url: "/UploadAction/UploadHandler.ashx",data: { operate: "deleteFile", file: path },success: function (objdata) {$("#fileSave").html(objdata);fileCount = $("#fileSave>div.uploadifyQueueItem").length;var less = 5 - fileCount;if (less <= 0) {$("#a_upload").attr("href", "javascript:");} else$("#a_upload").attr("href", "javascript:$('#uploadify').uploadifyUpload()");}});}</script></body></html>

后台的GetFile()方法:

/// <summary>/// 获取cookie附件信息/// </summary>/// <returns></returns>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('|');elsefileArray[0] = fileCookie.Value;foreach (string objFile in fileArray){if (!string.IsNullOrEmpty(objFile) && objFile.Contains(",")){string[] file = objFile.Split(',');strHtml.Append(@"<div class='uploadifyQueueItem'>");strHtml.Append(@"<div class='cancel'>");strHtml.Append("<a href='javascript:deleteFile(\"" + file[1] + "\")'></a>");//strHtml.Append(@"<img src='/Scripts/jquery.uploadify/cancel.png' border='0'>");strHtml.Append(@"</div>");strHtml.Append(@"<span class='fileName'>" + HttpUtility.UrlDecode(file[0]) + "</span><span class='percentage'> - 100%</span><div class='uploadifyProgress'>");strHtml.Append(@"<div class='uploadifyProgressBar' style='width: 100%;'>");strHtml.Append(@"</div>");strHtml.Append(@"</div>");strHtml.Append(@"</div>");}}}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('|');elsefileArray[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('|');elsefileArray[0] = fileCookie.Value;foreach (string objFile in fileArray){if (!string.IsNullOrEmpty(objFile) && objFile.Contains(",")){string[] file = objFile.Split(',');strHtml.Append(@"<div class='uploadifyQueueItem'>");strHtml.Append(@"<div class='cancel'>");strHtml.Append("<a href='javascript:deleteFile(\"" + file[1] + "\")'></a>");//strHtml.Append(@"<img src='/Scripts/jquery.uploadify-v2.1.0/cancel.png' border='0'>");strHtml.Append(@"</div>");strHtml.Append(@"<span class='fileName'>" + HttpUtility.UrlDecode(file[0]) + "</span><span class='percentage'> - 100%</span><div class='uploadifyProgress'>");strHtml.Append(@"<div class='uploadifyProgressBar' style='width: 100%;'>");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('|');elsefileArray[0] = fileCookie.Value;foreach (string objFile in fileArray){if (!string.IsNullOrEmpty(objFile) && objFile.Contains(",")){string[] file = objFile.Split(',');strHtml.Append(@"<div class='uploadifyQueueItem'>");strHtml.Append(@"<div class='cancel'>");strHtml.Append("<a href='javascript:deleteFile(\"" + file[1] + "\")'>");//strHtml.Append(@"<img src='/Scripts/jquery.uploadify-v2.1.0/cancel.png' border='0'></a>");strHtml.Append(@"</div>");strHtml.Append(@"<span class='fileName'>" + HttpUtility.UrlDecode(file[0]) + "</span><span class='percentage'> - 100%</span><div class='uploadifyProgress'>");strHtml.Append(@"<div class='uploadifyProgressBar' style='width: 100%;'>");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("/") + 1);//文件保存目录路径String savePath = "/upfiles/";//文件保存目录URLString 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 + "/";//定义允许上传的文件扩展名//最大文件大小int maxSize = 5242880;HttpPostedFile imgFile = context.Request.Files["imgFile"];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";}String fileName = imgFile.FileName;String fileExt = Path.GetExtension(fileName).ToLower();//创建文件夹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;imgFile.SaveAs(filePath);String fileUrl = saveUrl + newFileName;if (fileCookie != null){string strFile = fileCookie.Value;if (!string.IsNullOrEmpty(strFile))strFile = strFile + "|" + HttpUtility.UrlEncode(fileName) + "," + fileUrl;elsestrFile = 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,可以收获成果了:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章