栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > asp

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

asp 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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

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

2.上传页面UpFilePage.aspx

关键代码:




上传文件






#fileSave { padding-left:5px; padding-right:5px;}
#fileSave .uploadifyQueueItem{ width:530px;}
#fileQueue { padding-left:5px; padding-right:5px;}
#fileQueue .uploadifyQueueItem { width:530px;}
#uploadifyUploader { position:absolute; opacity:0;}
.uploadify-button{ height: 30px; line-height: 30px; width: 109px; text-align:center; border:0px; margin-bottom:5px; background:#ff6600; color:#fff;}
.cancel a { background:url(/jquery.uploadify/cancel.png) no-repeat center center; display:inline-block; width:16px; height:16px;}









后台的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 + "/";

//定义允许上传的文件扩展名


//最大文件大小
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;
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,可以收获成果了:

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/56157.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号