ASP.NET帶進度條多文件上傳

一、資源

1)Uploadify v2.1.0,可以到這裏下載:www.uploadify.com。

2)JQuery EasyUI ,下載地址:http://jquery-easyui.wikidot.com/download

 

二、預覽

1)初始界面 

 

2) 點擊【BROWSE】選擇多文件

 

3) 選擇的文件列表,點擊【全部上傳】開始上傳文件隊列

 

 三、代碼

1)解壓jquery.uploadify-v2.1.0.zip,複製example/index.php的代碼,對應粘貼到你的頁面(HTML或ASPX),注意拷貝相應的CSS、JS和SWF文件到你的項目對應目錄

2)解壓 JQuery EasyUI.zip,拷貝相應的CSS、JS文件到你的項目對應目錄,並在你的頁面中的<title></title>標籤中添加引用

HTML:

[xhtml:nogutter] view plaincopy
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  2. <html xmlns="http://www.w3.org/1999/xhtml">  
  3. <head>  
  4.     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
  5.     <title>多文件上傳 - 可設置多文件還是單文件上傳,以及上傳文件的大小</title>  
  6.     <!--JQuery-->  
  7.     <mce:script type="text/javascript" src="scripts/jquery-1.4.2.min.js" mce_src="scripts/jquery-1.4.2.min.js"></mce:script>  
  8.     <!--JQuery EasyUI-->  
  9.     <link href="css/easyui/themes/default/easyui.css" mce_href="css/easyui/themes/default/easyui.css" rel="stylesheet" type="text/css" />  
  10.     <link href="css/easyui/themes/icon.css" mce_href="css/easyui/themes/icon.css" rel="stylesheet" type="text/css" />  
  11.     <mce:script type="text/javascript" src="scripts/jquery.easyui.min.js" mce_src="scripts/jquery.easyui.min.js"></mce:script>  
  12.     <!--MultiUpload-->  
  13.     <link href="css/default.css" mce_href="css/default.css" rel="stylesheet" type="text/css" />  
  14.     <link href="css/uploadify.css" mce_href="css/uploadify.css" rel="stylesheet" type="text/css" />  
  15.     <mce:script type="text/javascript" src="scripts/swfobject.js" mce_src="scripts/swfobject.js"></mce:script>  
  16.     <mce:script type="text/javascript" src="scripts/jquery.uploadify.v2.1.0.min.js" mce_src="scripts/jquery.uploadify.v2.1.0.min.js"></mce:script>  
  17.     <mce:script type="text/javascript"><!--  
  18.         $(document).ready(function () {  
  19.             $("#uploadify").uploadify({  
  20.                 'uploader': 'Flash/uploadify.swf',  
  21.                 'script': 'UploadHandler.ashx',  
  22.                 'cancelImg': 'Images/cancel.png',  
  23.                 'folder': 'Uploads',  
  24.                 'queueID': 'fileQueue',  
  25.                 //'fileDesc': '*.rar;*.jpg;*.gif',  
  26.                 //'fileExt': '*.rar;*.jpg;*.gif',  
  27.                 'sizeLimit': '2097152', //2M  
  28.                 'auto': false,  
  29.                 'multi': true,  
  30.                 'onError': function (a, b, c, d) {  
  31.                     if (d.status == 404)  
  32.                         alert('Could not find upload script.');  
  33.                     else if (d.type === "HTTP")  
  34.                         alert('error ' + d.type + ": " + d.status);  
  35.                     else if (d.type === "File Size")  
  36.                         alert(c.name + ' ' + d.type + ' Limit: ' + Math.round(d.sizeLimit / 1024) + 'KB');  
  37.                     else  
  38.                         alert('error ' + d.type + ": " + d.info);  
  39.                 }  
  40.             });  
  41.         });  
  42. // --></mce:script>  
  43. </head>  
  44. <body>  
  45.     <div class="easyui-tabs" style="width: 400px; height: 300px;padding-bottom:5px">  
  46.         <div title="上傳文件列表" id="fileQueue" style="padding: 10px;" mce_style="padding: 10px;">  
  47.               
  48.         </div>  
  49.         <!--<div title="已上傳文件" id="fileUploaded" closable="false" style="padding: 10px;" mce_style="padding: 10px;">  
  50.               
  51.         </div>-->  
  52.     </div>  
  53.     <input type="file" name="uploadify" id="uploadify" />  
  54.     <p>  
  55.         <a href="javascript:$('#uploadify').uploadifyUpload()" mce_href="javascript:$('#uploadify').uploadifyUpload()">全部上傳</a><a href="javascript:$('#uploadify').uploadifyClearQueue()" mce_href="javascript:$('#uploadify').uploadifyClearQueue()">  
  56.             全部取消</a>  
  57.     </p>  
  58. </body>  
  59. </html>  
 

 

UploadHandler.ashx文件代碼:

[c-sharp:nogutter] view plaincopy
  1. <%@ WebHandler Language="C#" Class="UploadHandler" %>  
  2. using System;  
  3. using System.IO;   
  4. using System.Net;  
  5. using System.Web;   
  6. public class UploadHandler : IHttpHandler  
  7. {  
  8.     public void ProcessRequest(HttpContext  context)   
  9.     {  
  10.         context.Response.ContentType = "text/plain";   
  11.         context.Response.Charset = "utf-8";  
  12.           
  13.         //獲取上傳文件隊列  
  14.         HttpPostedFile oFile = context.Request.Files["Filedata"];  
  15.         if (oFile != null)  
  16.         {  
  17.             string topDir = context.Request["folder"];  
  18.             //創建頂級目錄  
  19.             if (!Directory.Exists(HttpContext.Current.Server.MapPath(topDir)))  
  20.             {  
  21.                 Directory.CreateDirectory(HttpContext.Current.Server.MapPath(topDir));  
  22.             }   
  23.               
  24.             //當天上傳的文件放到已當天日期命名的文件夾中  
  25.             string dateFolder = HttpContext.Current.Server.MapPath(topDir) + "//" + DateTime.Now.Date.ToString("yyyy-MM-dd");  
  26.             if (!Directory.Exists(dateFolder))  
  27.             {  
  28.                 Directory.CreateDirectory(dateFolder);  
  29.             }  
  30.             oFile.SaveAs(dateFolder + "//" + oFile.FileName);  
  31.             context.Response.Write("1");  
  32.               
  33.         }   
  34.         else   
  35.         {   
  36.             context.Response.Write("0");   
  37.         }  
  38.     }  
  39.     public bool IsReusable  
  40.     {   
  41.         get  { return false; }  
  42.     }   
  43. }  
 

 

下一篇談一下如何讓上傳的文件跟特定的數據庫信息關聯。 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章