ASP.NET使用SWFUpload上傳檔案教學

前陣子有人提到這個上傳工具,小弟沒玩過就給它抓下來試試
SWFUpload可以支援多檔上傳功能,還不錯用,小弟分享一下試用的結果

首先要將官網的Demo Sample抓下來,如下所示:

SWFUpload下載網址:http://swfupload.googlecode.com
SWFUpload下載檔案:SWFUpload-Samples v2.1.0.Release.zip

在\SWFUpload Samples v2.1.0\demos\applicationdemo.net目錄裡有下列檔案

接下來只要修改Default這支程式就可以了..我只增加了儲存選取檔案的功能..

清除目前選取檔案的功能..更多的功能就要自己去修改了...

asp.net(c#)

Default.aspx

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>  
  2.   
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  4.   
  5. <html xmlns="http://www.w3.org/1999/xhtml" >  
  6. <head runat="server">  
  7.     <title>SWFUpload Revision v2.1.0 Application Demo (ASP.Net 2.0)</title>  
  8.     <link href="css/default.css" rel="stylesheet" type="text/css" />  
  9.     <script type="text/javascript" src="swfupload/swfupload.js"></script>  
  10.     <script type="text/javascript" src="js/handlers.js"></script>  
  11.     <script type="text/javascript">  
  12.         var swfu;   
  13.         window.onload = function () {   
  14.             swfu = new SWFUpload({   
  15.                 // Backend Settings   
  16.                 upload_url: "upload.aspx",  // Relative to the SWF file   
  17.                 post_params : {   
  18.                     "ASPSESSID" : "<%=Session.SessionID %>"   
  19.                 },   
  20.   
  21.                 // File Upload Settings   
  22.                 file_size_limit : "2048",   // 2MB   
  23.                 file_types : "*.jpg",   
  24.                 file_types_description : "JPG Images",   
  25.                 file_upload_limit : "0",    // Zero means unlimited   
  26.   
  27.                 // Event Handler Settings - these functions as defined in Handlers.js   
  28.                 //  The handlers are not part of SWFUpload but are part of my website and control how   
  29.                 //  my website reacts to the SWFUpload events.   
  30.                 file_queue_error_handler : fileQueueError,   
  31.                 file_dialog_complete_handler : fileDialogComplete,   
  32.                 upload_progress_handler : uploadProgress,   
  33.                 upload_error_handler : uploadError,   
  34.                 upload_success_handler : uploadSuccess,   
  35.                 upload_complete_handler : uploadComplete,   
  36.   
  37.                 // Flash Settings   
  38.                 flash_url : "swfupload/swfupload_f9.swf",   // Relative to this file   
  39.   
  40.                 custom_settings : {   
  41.                     upload_target : "divFileProgressContainer"   
  42.                 },   
  43.   
  44.                 // Debug Settings   
  45.                 debug: false   
  46.             });   
  47.         }   
  48.     </script>  
  49. </head>  
  50. <body>  
  51.     <form id="form1" runat="server">  
  52.         <div id="header">  
  53.             <h1 id="logo"><a href="../">SWFUpload</a></h1>  
  54.             <div id="version">v2.1.0 Beta</div>  
  55.         </div>  
  56.   
  57.   
  58.     <div id="content">  
  59.                 <h2>Application Demo (ASP.Net 2.0)</h2>  
  60.            
  61.         <div id="swfu_container" style="margin: 0px 10px;">  
  62.             <div>  
  63.                 <button id="btnBrowse" type="button" style="padding: 5px;" onclick="swfu.selectFiles(); this.blur();"><img src="images/page_white_add.png" style="padding-right: 3px; vertical-align: bottom;" alt="Add Icon" />Select Images <span style="font-size: 7pt;">(2 MB Max)</span></button>  
  64.                 <asp:Button ID="btnSave" runat="server" OnClick="btnSave_Click" Text="Save Select Images" Width="150px" />  
  65.                 <asp:Button ID="btnClear" runat="server" Text="Clear Select Images" OnClick="btnClear_Click" Width="150px" /></div>  
  66.             <div id="divFileProgressContainer" style="height: 75px;"></div>  
  67.             <div id="thumbnails"></div>  
  68.         </div>  
  69.         </div>  
  70.     </form>  
  71. </body>  
  72. </html>  

Default.aspx.cs

  1. using System;   
  2. using System.Data;   
  3. using System.Configuration;   
  4. using System.Collections;   
  5. using System.Web;   
  6. using System.Web.Security;   
  7. using System.Web.UI;   
  8. using System.Web.UI.WebControls;   
  9. using System.Web.UI.WebControls.WebParts;   
  10. using System.Web.UI.HtmlControls;   
  11. using System.Collections.Generic;   
  12. using System.IO;   
  13.   
  14. public partial class _Default : System.Web.UI.Page   
  15. {   
  16.     protected void Page_Load(object sender, EventArgs e)   
  17.     {   
  18.         // Clear the user's session   
  19.         if (!IsPostBack)   
  20.         {   
  21.             Session.Clear();   
  22.         }   
  23.     }   
  24.     protected void btnSave_Click(object sender, EventArgs e)   
  25.     {   
  26.         if (Session["file_info"] != null)   
  27.         {   
  28.             List<Thumbnail> thumbnails = Session["file_info"as List<Thumbnail>;   
  29.   
  30.             string UploadPath = Server.MapPath("upload/");   
  31.   
  32.             foreach (Thumbnail img in thumbnails)   
  33.             {   
  34.                 FileStream fs = new FileStream(UploadPath + img.ID + ".jpg", FileMode.Create);   
  35.                 BinaryWriter bw = new BinaryWriter(fs);   
  36.                 bw.Write(img.Data);   
  37.                 bw.Close();   
  38.                 fs.Close();   
  39.             }   
  40.   
  41.             Session.Clear();   
  42.         }   
  43.     }   
  44.     protected void btnClear_Click(object sender, EventArgs e)   
  45.     {   
  46.         Session.Clear();   
  47.     }   
  48. }  

執行結果:

參考網址:http://www.cnblogs.com/icejd/archive/2008/12/23/1360214.html

原文地址:https://www.cnblogs.com/icejd/p/1293468.html