一个很好的无刷新多文件上传代码
1.在网上找的,大家可以改改,思路很好.
2.主页面的核心代码:
3.上传页面代码
ASPX:
2.主页面的核心代码:
<script type="text/javascript">
$(document).ready(function(){
for(var i=0;i<5;i++)
{
uploadcreate($("#uploadbox"),i);
}
});
var hideDiv = function(idName){
$("#"+idName).fadeOut("fast");
};
//是否显示上传后的图片
var isshowpic = true;
var uploadshowpic = function(el){
isshowpic = !(isshowpic);
if(isshowpic)
{
el.html("图片显示关闭");
}
else
{
el.html("图片显示开启");
}
};
//载入中的GIF动画
var loadingUrl = "images/ajax-loader.gif";
//选择文件后的事件,iframe里面调用的
var uploading = function(imgsrc,itemid){
var el = $("#uploading"+itemid);
$("#ifUpload"+itemid).fadeOut("fast");
el.fadeIn("fast");
el.html("<img src='"+loadingUrl+"' align='absmiddle' /> 上传中");
return el;
};
//重新上传方法
var uploadinit = function(itemid){
currentItemID ++;
$("#uploading"+itemid).fadeOut("fast",function(){
$("#ifUpload"+itemid).fadeIn("fast");
$("#panelViewPic"+itemid).fadeOut("fast");
});
};
//上传时程序发生错误时,给提示,并返回上传状态
var uploaderror = function(itemid){
alert("程序异常,"+itemid+"项上传未成功。");
uploadinit();
};
//上传成功后的处理
var uploadsuccess = function(newpath,itemid){
$("#uploading"+itemid).html("文件上传成功. <a href='javascript:void(0);' onclick='uploadinit("+itemid+");'>[重新上传]</a>");
if(isshowpic)
{
$("#panelViewPic"+itemid).html("<a href='"+newpath+"' title='点击查看大图' target='_blank'><img src='"+newpath+"' alt='' style='width:300px;' /></a>");
$("#panelViewPic"+itemid).fadeIn("fast");
}
};
var currentItemID = 0; //用于存放共有多少个上传控件了
//创建一个上传控件
var uploadcreate = function(el,itemid){
currentItemID ++;
if(itemid == null)
{
itemid = currentItemID;
}
var strContent = "<div class='uploadcontrol'><iframe src=\"upload.aspx?id="+itemid+"\" id=\"ifUpload"+itemid+"\" frameborder=\"no\" scrolling=\"no\" style=\"width:400px; height:28px;\"></iframe>";
strContent += "<div class=\"uploading\" id=\"uploading"+itemid+"\" style=\"display:none;\" ></div>";
strContent += "<div class=\"image\" id=\"panelViewPic"+itemid+"\" style=\"display:none;\"></div></div>";
el.append(strContent);
};
</script>
</head>
<body>
<div id="tipbox" class="box tooltip">
<a href="#" onclick="hideDiv('tipbox');">[关闭]</a>
<div class="content">
<h1>AjaxUpload - 多文件无刷新上传源代码 v1.0</h1>
<p>作者:李华顺 <a href="http://huacn.cnblogs.com" target="_blank">http://huacn.cnblogs.com</a></p>
</div>
</div>
<div id="toolbox" class="tooltip box">
<a href="#" onclick="uploadcreate($('#uploadbox'));">添加一个新上传控件</a> <a href="#" onclick="uploadshowpic($(this));">图片显示关闭</a>
</div>
<div id="uploadbox" class="box">
</div>
</body>
$(document).ready(function(){
for(var i=0;i<5;i++)
{
uploadcreate($("#uploadbox"),i);
}
});
var hideDiv = function(idName){
$("#"+idName).fadeOut("fast");
};
//是否显示上传后的图片
var isshowpic = true;
var uploadshowpic = function(el){
isshowpic = !(isshowpic);
if(isshowpic)
{
el.html("图片显示关闭");
}
else
{
el.html("图片显示开启");
}
};
//载入中的GIF动画
var loadingUrl = "images/ajax-loader.gif";
//选择文件后的事件,iframe里面调用的
var uploading = function(imgsrc,itemid){
var el = $("#uploading"+itemid);
$("#ifUpload"+itemid).fadeOut("fast");
el.fadeIn("fast");
el.html("<img src='"+loadingUrl+"' align='absmiddle' /> 上传中");
return el;
};
//重新上传方法
var uploadinit = function(itemid){
currentItemID ++;
$("#uploading"+itemid).fadeOut("fast",function(){
$("#ifUpload"+itemid).fadeIn("fast");
$("#panelViewPic"+itemid).fadeOut("fast");
});
};
//上传时程序发生错误时,给提示,并返回上传状态
var uploaderror = function(itemid){
alert("程序异常,"+itemid+"项上传未成功。");
uploadinit();
};
//上传成功后的处理
var uploadsuccess = function(newpath,itemid){
$("#uploading"+itemid).html("文件上传成功. <a href='javascript:void(0);' onclick='uploadinit("+itemid+");'>[重新上传]</a>");
if(isshowpic)
{
$("#panelViewPic"+itemid).html("<a href='"+newpath+"' title='点击查看大图' target='_blank'><img src='"+newpath+"' alt='' style='width:300px;' /></a>");
$("#panelViewPic"+itemid).fadeIn("fast");
}
};
var currentItemID = 0; //用于存放共有多少个上传控件了
//创建一个上传控件
var uploadcreate = function(el,itemid){
currentItemID ++;
if(itemid == null)
{
itemid = currentItemID;
}
var strContent = "<div class='uploadcontrol'><iframe src=\"upload.aspx?id="+itemid+"\" id=\"ifUpload"+itemid+"\" frameborder=\"no\" scrolling=\"no\" style=\"width:400px; height:28px;\"></iframe>";
strContent += "<div class=\"uploading\" id=\"uploading"+itemid+"\" style=\"display:none;\" ></div>";
strContent += "<div class=\"image\" id=\"panelViewPic"+itemid+"\" style=\"display:none;\"></div></div>";
el.append(strContent);
};
</script>
</head>
<body>
<div id="tipbox" class="box tooltip">
<a href="#" onclick="hideDiv('tipbox');">[关闭]</a>
<div class="content">
<h1>AjaxUpload - 多文件无刷新上传源代码 v1.0</h1>
<p>作者:李华顺 <a href="http://huacn.cnblogs.com" target="_blank">http://huacn.cnblogs.com</a></p>
</div>
</div>
<div id="toolbox" class="tooltip box">
<a href="#" onclick="uploadcreate($('#uploadbox'));">添加一个新上传控件</a> <a href="#" onclick="uploadshowpic($(this));">图片显示关闭</a>
</div>
<div id="uploadbox" class="box">
</div>
</body>
3.上传页面代码
ASPX:
<script type="text/javascript">
var uploadSelect = function(el){
el.fadeOut("show");
parent.uploading(document.getElementById("<%=file1.ClientID %>").value,'<%=itemID %>');
$("#<%=frmUpload.ClientID %>").submit();
};
</script>
</head>
<body>
<form runat="server" id="frmUpload" method="post" enctype="multipart/form-data">
<input type="file" runat="server" id="file1" size="40" onchange="uploadSelect($(this));" />
</form>
</body>
C#代码:var uploadSelect = function(el){
el.fadeOut("show");
parent.uploading(document.getElementById("<%=file1.ClientID %>").value,'<%=itemID %>');
$("#<%=frmUpload.ClientID %>").submit();
};
</script>
</head>
<body>
<form runat="server" id="frmUpload" method="post" enctype="multipart/form-data">
<input type="file" runat="server" id="file1" size="40" onchange="uploadSelect($(this));" />
</form>
</body>
public partial class upload : System.Web.UI.Page
{
string picPath = "";
string picServer = "/upload";
protected string itemID = "";
protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString["id"] != null)
{
itemID = Request.QueryString["id"];
}
if (IsPostBack)
{
picPath = Server.MapPath(@"~\upload");
doUpload();
}
}
protected void doUpload()
{
try
{
HttpPostedFile file = file1.PostedFile;
string strNewPath = GetSaveFilePath() + GetExtension(file.FileName);
file.SaveAs(picPath+strNewPath);
string urlPath = picServer + strNewPath;
urlPath = "."+urlPath.Replace("\\", "/");
WriteJs("parent.uploadsuccess('" + urlPath + "','" + itemID + "'); ");
}
catch (Exception ex)
{
WriteJs("parent.uploaderror();");
}
}
private string GetExtension(string fileName)
{
try
{
int startPos = fileName.LastIndexOf(".");
string ext = fileName.Substring(startPos, fileName.Length - startPos);
return ext;
}
catch (Exception ex)
{
WriteJs("parent.uploaderror('" + itemID + "');");
return string.Empty;
}
}
private string GetSaveFilePath()
{
try
{
DateTime dateTime = DateTime.Now;
string yearStr = dateTime.Year.ToString(); ;
string monthStr = dateTime.Month.ToString();
string dayStr = dateTime.Day.ToString();
string hourStr = dateTime.Hour.ToString();
string minuteStr = dateTime.Minute.ToString();
string dir = dateTime.ToString(@"\\yyyyMMdd");
if (!Directory.Exists(picPath + dir))
{
Directory.CreateDirectory(picPath + dir);
}
return dir + dateTime.ToString("\\\\yyyyMMddhhmmssffff");
}
catch (Exception ex)
{
WriteJs("parent.uploaderror();");
return string.Empty;
}
}
protected void WriteJs(string jsContent)
{
this.Page.RegisterStartupScript("writejs","<script type='text/javascript'>"+ jsContent+"</script>");
}
}
2.下载地址:https://files.cnblogs.com/ForFreeDom/ajaxupload_src.rar{
string picPath = "";
string picServer = "/upload";
protected string itemID = "";
protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString["id"] != null)
{
itemID = Request.QueryString["id"];
}
if (IsPostBack)
{
picPath = Server.MapPath(@"~\upload");
doUpload();
}
}
protected void doUpload()
{
try
{
HttpPostedFile file = file1.PostedFile;
string strNewPath = GetSaveFilePath() + GetExtension(file.FileName);
file.SaveAs(picPath+strNewPath);
string urlPath = picServer + strNewPath;
urlPath = "."+urlPath.Replace("\\", "/");
WriteJs("parent.uploadsuccess('" + urlPath + "','" + itemID + "'); ");
}
catch (Exception ex)
{
WriteJs("parent.uploaderror();");
}
}
private string GetExtension(string fileName)
{
try
{
int startPos = fileName.LastIndexOf(".");
string ext = fileName.Substring(startPos, fileName.Length - startPos);
return ext;
}
catch (Exception ex)
{
WriteJs("parent.uploaderror('" + itemID + "');");
return string.Empty;
}
}
private string GetSaveFilePath()
{
try
{
DateTime dateTime = DateTime.Now;
string yearStr = dateTime.Year.ToString(); ;
string monthStr = dateTime.Month.ToString();
string dayStr = dateTime.Day.ToString();
string hourStr = dateTime.Hour.ToString();
string minuteStr = dateTime.Minute.ToString();
string dir = dateTime.ToString(@"\\yyyyMMdd");
if (!Directory.Exists(picPath + dir))
{
Directory.CreateDirectory(picPath + dir);
}
return dir + dateTime.ToString("\\\\yyyyMMddhhmmssffff");
}
catch (Exception ex)
{
WriteJs("parent.uploaderror();");
return string.Empty;
}
}
protected void WriteJs(string jsContent)
{
this.Page.RegisterStartupScript("writejs","<script type='text/javascript'>"+ jsContent+"</script>");
}
}