• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

[荐]Javascript与asp.net实现Ajax多文件无刷新上传

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
︶ㄣ九蜘蛛:一直对AJAX情有独钟,但由于工作上的事情和自己的事情暂时无法认真的系统学习一下。
中午有空的时候就到园里来逛逛 , 偷点果孖自己珍藏起,供自己以学习参考用 ^_______^~,
下文来自:http://www.cnblogs.com/huacn/archive/2007/07/16/819196.html  十分感谢!!


方法都整理好,可以随意添加多个上传控件,只要调用一个方法就可以了,为了便于阅读我没有把JS独立出来,以后真正用到项目上的时候再提出来,我在每个方法上面都写了注释,具体可以看代码部分,现在一直在用JQuery,它提供的方法太好用的,省了很多事。
此方法主要是通过iFrame调用上传页的控件来实现的,具体请看下面的代码。





无刷新上传主要的HTML代码(upload.html):
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<title>AjaxUpload</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="scripts/jquery.js"></script>
<script type="text/javascript" src="scripts/interface.js"></script>
<style type="text/css" media="all">
*
{
    margin
:0;
    padding
:0;
}

img
{border:none;}

ul
{
    list-style-type
:none;
}

ul li
{
    padding
:2px 4px;
}

body
{
    font-family
: 宋体, 黑体,verdana, Arial;
    font-size
:12px;
    color
:#333;
    background
:#DDDDDD;
    margin
:30px;
    padding
:0;
}

.box
{
    border
:1px solid #CCC;
    background
:#FFF;
    padding
:8px;
    margin
:5px;
    clear
:both;
}

.title 
{
    background
:#F0F0F0;padding:5px;
    font-weight
:bold;
}

.tooltip
{
    background
:#F0F0F0;
    border-color
:#bbb;
}

.tooltip h1 
{
    color
:#A8DF00;
    font-family
: 微软雅黑,黑体,宋体,verdana, Arial;
}

.titlebutton
{
    float
:right;
}

.uploading
{
    background
:#FFF;
    color
:#444;
    text-align
:left;
    width
:500px;
    padding
:4px;
}

.image
{
    border
:1px solid #ddd;
    margin
:2px;
    padding
:1px;
    display
:inline;
    width
:300px;
}

.uploadcontrol 
{
    margin
:4px 0;
    border-bottom
:1px solid #F0F0F0;
}
</style>
<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">
    
<href="#" onclick="hideDiv('tipbox');">[关闭]</a>
    
<div class="content">
        
<h1>AjaxUpload - 多文件无刷新上传源代码 v1.0</h1>
        
<p>作者:李华顺 <href="http://huacn.cnblogs.com" target="_blank">http://huacn.cnblogs.com</a></p>
    
</div> 
</div>
<div id="toolbox" class="tooltip box">
    
<href="#" onclick="uploadcreate($('#uploadbox'));">添加一个新上传控件</a> <href="#" onclick="uploadshowpic($(this));">图片显示关闭</a>
</div>
<div id="uploadbox" class="box">
    
</div>

</body>

</html>

上传功能的页面代码(upload.aspx):
<%@ Page Language

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
尚未在Web服务器上注册ASP.NET4.5。安装VS15后的问题(转)发布时间:2022-07-10
下一篇:
学习ASP.NETStarterKit[Portal](20050630)发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap