在ASP.NET中利用SlickUpload来上传大文件

SlickUpload的介绍
最近在看《ASP.NET MVC2 IN ACTION》英文版时发现里面介绍到一个在ASP.NET中上传大文件的ASP.NET控件,叫SlickUpload,软件的介绍下载和介绍网址是:http://krystalware.com/Products/SlickUpload/。
我们知道,在ASP.NET中对于上传文件可以利用FileUpload控件或者<input type="file" name="file"/>这样的方式就可以上传,但是利用这样的方式上传存在一些缺点,比如默认一次只能上传一个文件及上传的文件的大小也有限制,除此之外,利用这种方式上传不能以直观的方式显示上传的进度情况,于是有了各种各样的第三方解决方案,比如SWFUpload等。不过因为在最近的版本中ADOBE公司增强了权限控制,所以SWFUpload也有些不太灵光了,这里介绍另外一种文件上传控件SlickUpload。SlickUpload是一款共享软件,它的购买地址是http://krystalware.com/purchase/,如果公司有经常上传超大文件需求的,不妨购买。
SlickUpload的原理如下图:

可以看出它的处理流程如下:
对于请求中的每个文件SlickUpload都会调用定义好的上传文件流供应者来写文件数据,SlickUpload支持两种内置的文件处理方式和用户自定义的处理方式:
文件系统,即将文件保存到服务器文件下;
SQL,即将文件保存到SQL服务器中;
根据用户逻辑的自定义方式;
SlickUpload可以在ASP.NET WebForm和ASP.NET MVC及ASP.NET AJAX项目中运行,可以以带进度显示上传超过上G的文件。
在不同环境下最大可支持上传的文件大小:
IIS 5:最大可支持上传2 GB
IIS 6:最大支持上传4 GB
IIS 7传统模式:最大支持上传4 GB
IIS 7 集成模式:最大支持上传2 GB
VS.NET集成WebDev server环境下:最大支持2 GB
当然上面的最大支持需要在web.config文件中的<httpRuntime>节点的maxRequestLength属性。

 

SlickUpload的安装
1.程序集的安装
首先请将Krystalware.SlickUpload.dll拷贝到Web项目的bin目录下,这个程序集包含了SlickUpload的HttpModule模块。
2.添加SlickUpload控件到VS的控件工具箱(周公的VS版本为VS2008SP1)
步骤如下:
2.1用VS打开Web项目或者包含Web项目的解决方案。
2.2在VS上的控件箱空白处鼠标右键点击,选择“选择项”,出现如下图所示的界面:

2.3在打开的对话框中选择“浏览”,如下图所示:

找到Krystalware.SlickUpload.dll所在的位置,然后点击确定,这样就完成了将SlickUpload添加到控件箱。如下图所示:

3.在web.config中配置
3.1配置configSections节点
打开web.config文件找到configSections节点(如果没有的话请在<configuration>元素下添加),在configSections节点下添加如下内容:

 

 
  1. <sectionGroup name="slickUpload">
  2. <section name="uploadParser" type="Krystalware.SlickUpload.Configuration.NameValueConfigurationSectionHandler, Krystalware.SlickUpload" />
  3. <section name="uploadStreamProvider" type="Krystalware.SlickUpload.Configuration.NameValueConfigurationSectionHandler, Krystalware.SlickUpload" />
  4. <section name="statusManager" type="Krystalware.SlickUpload.Configuration.StatusManagerConfigurationSectionHandler, Krystalware.SlickUpload" />
  5. </sectionGroup>

<sectionGroup name="slickUpload"> <section name="uploadParser" type="Krystalware.SlickUpload.Configuration.NameValueConfigurationSectionHandler, Krystalware.SlickUpload" /> <section name="uploadStreamProvider" type="Krystalware.SlickUpload.Configuration.NameValueConfigurationSectionHandler, Krystalware.SlickUpload" /> <section name="statusManager" type="Krystalware.SlickUpload.Configuration.StatusManagerConfigurationSectionHandler, Krystalware.SlickUpload" /> </sectionGroup>

 

添加slickUpload节点

在<configuration>元素下添加slickUpload节点,如下:

 

 
  1. <slickUpload>
  2. <uploadStreamProvider location="~/Files" existingAction="Overwrite" />
  3. </slickUpload>

<slickUpload> <uploadStreamProvider location="~/Files" existingAction="Overwrite" /> </slickUpload>

 

其中的location表示文件上传后的存放位置,existingAction表示如果在服务端已经存在同名文件如何处理。
system.web/httpModules节点配置
在system.web节点下添加如下内容:

 

 
  1. <httpModules>
  2. <add name="HttpUploadModule" type="Krystalware.SlickUpload.HttpUploadModule, Krystalware.SlickUpload" />
  3. </httpModules>

<httpModules> <add name="HttpUploadModule" type="Krystalware.SlickUpload.HttpUploadModule, Krystalware.SlickUpload" /> </httpModules>

 

如果在web.config中已经存在system.web/httpModules节点,则只需将<add ..../>部分添加到这个节点下即可。
system.web/httpHandlers节点配置
在system.web节点下添加如下内容:

 

 
  1. <httpHandlers>
  2. <add path="SlickUpload.axd" verb="GET,POST" type="Krystalware.SlickUpload.SlickUploadHandler, Krystalware.SlickUpload" />
  3. </httpHandlers>

<httpHandlers> <add path="SlickUpload.axd" verb="GET,POST" type="Krystalware.SlickUpload.SlickUploadHandler, Krystalware.SlickUpload" /> </httpHandlers>

 

如果在web.config中已经存在system.web/httpModules节点,则只需将<add ..../>部分添加到这个节点下即可。
system.web/httpRuntime节点配置
在system.web节点下添加如下内容:

 

 
  1. <httpRuntime maxRequestLength="1024000" executionTimeout="600" />

<httpRuntime maxRequestLength="1024000" executionTimeout="600" />

 

以上配置表示允许最大上传1GB的文件(maxRequestLength的单位为MB),程序执行超时时间为10分钟(executionTimeout的单位为秒)。
经过如下配置之后就可以在我们的ASP.NET项目中使用SlickUpload项目了。

SlickUpload的使用

快速上手指南
下面是一个简单使用的例子,代码如下:

 

view plain copy to clipboard print ?
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="SlickUploadQuickStart.aspx.cs" Inherits="SlickUploadQuickStart" %>
  2.  
  3. <%@ Register assembly="Krystalware.SlickUpload" namespace="Krystalware.SlickUpload.Controls" tagprefix="kw" %>
  4.  
  5. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  6.  
  7. <html xmlns="http://www.w3.org/1999/xhtml">
  8. <head runat="server">
  9. <title>SlickUpload Quick Start</title>
  10. <mce:script type="text/javascript"><!--
  11. function cancelUpload() {
  12. kw.get("<%=SlickUpload1.ClientID %>").cancel();
  13. }
  14. // --></mce:script>
  15. </head>
  16. <body>
  17. <h1>SlickUpload Quick Start</h1>
  18. <form id="form1" runat="server">
  19.  
  20. <kw:SlickUpload ID="SlickUpload1" runat="server" ShowDuringUploadElements="cancelButton"
  21. onuploadcomplete="SlickUpload1_UploadComplete" Width="480px">
  22. <downlevelselectortemplate>
  23. <input type="file" />
  24. </downlevelselectortemplate>
  25. <uplevelselectortemplate>
  26. <input type="button" value="选择文件" />
  27. </uplevelselectortemplate>
  28. <filetemplate>
  29. <kw:FileListRemoveLink runat="server">
  30. [x] 删除</kw:FileListRemoveLink>
  31. <kw:FileListFileName runat="server" />
  32. <kw:FileListValidationMessage runat="server" ForeColor="Red" />
  33. </filetemplate>
  34. <progresstemplate>
  35. <table width="100%">
  36. <tr>
  37. <td>
  38. 文件上传中
  39. <kw:UploadProgressElement runat="server" Element="FileCountText">
  40. </kw:UploadProgressElement>
  41. ,
  42. <kw:UploadProgressElement runat="server" Element="ContentLengthText">
  43. (calculating)</kw:UploadProgressElement>
  44. .
  45. </td>
  46. </tr>
  47. <tr>
  48. <td>
  49. 当前正在上传:
  50. <kw:UploadProgressElement runat="server" Element="CurrentFileName">
  51. </kw:UploadProgressElement>
  52. , 进度
  53. <kw:UploadProgressElement runat="server" Element="CurrentFileIndex">
  54. </kw:UploadProgressElement>
  55. /
  56. <kw:UploadProgressElement runat="server" Element="FileCount">
  57. </kw:UploadProgressElement>
  58. .
  59. </td>
  60. </tr>
  61. <tr>
  62. <td>
  63. 上传速度:
  64. <kw:UploadProgressElement runat="server" Element="SpeedText">
  65. (calculating)</kw:UploadProgressElement>
  66. .
  67. </td>
  68. </tr>
  69. <tr>
  70. <td>
  71. 估计剩余时间:
  72. <kw:UploadProgressElement runat="server" Element="TimeRemainingText">
  73. (calculating)</kw:UploadProgressElement>
  74. </td>
  75. </tr>
  76. <tr>
  77. <td>
  78. <div style="border:1px solid #008800;height:1.5em;position:relative">
  79. <kw:UploadProgressBarElement runat="server"
  80. style="background-color:#00ee00;width:0;height:1.5em">
  81. </kw:UploadProgressBarElement>
  82. <div style="text-align:center;position:absolute;top:.15em;width:100%">
  83. <kw:UploadProgressElement runat="server" Element="PercentCompleteText">
  84. (calculating)</kw:UploadProgressElement>
  85. </div>
  86. </div>
  87. </td>
  88. </tr>
  89. </table>
  90. </progresstemplate>
  91. </kw:SlickUpload>
  92.  
  93. <hr />
  94. <asp:Button ID="Button1" runat="server" Text="上传" />
  95. <asp:Button ID="cancelButton" runat="server"
  96. onclientclick="cancelUpload();return false;" Text="Cancel" style="display:none" mce_style="display:none" />
  97. <br />
  98. <br />
  99. <asp:Label ID="uploadResult" runat="server"></asp:Label>
  100. <asp:Repeater ID="uploadFileList" runat="server" EnableViewState="False">
  101. <FooterTemplate></ul></FooterTemplate>
  102. <ItemTemplate><li><%# DataBinder.Eval(Container.DataItem, "ClientName") %></li></ItemTemplate>
  103. <HeaderTemplate><ul></HeaderTemplate>
  104. </asp:Repeater>
  105. </form>
  106. </body>
  107. </html>

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="SlickUploadQuickStart.aspx.cs" Inherits="SlickUploadQuickStart" %> <%@ Register assembly="Krystalware.SlickUpload" namespace="Krystalware.SlickUpload.Controls" tagprefix="kw" %> <!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 runat="server"> <title>SlickUpload Quick Start</title> <mce:script type="text/javascript"><!-- function cancelUpload() { kw.get("<%=SlickUpload1.ClientID %>").cancel(); } // --></mce:script> </head> <body> <h1>SlickUpload Quick Start</h1> <form id="form1" runat="server"> <kw:SlickUpload ID="SlickUpload1" runat="server" ShowDuringUploadElements="cancelButton" onuploadcomplete="SlickUpload1_UploadComplete" Width="480px"> <downlevelselectortemplate> <input type="file" /> </downlevelselectortemplate> <uplevelselectortemplate> <input type="button" value="选择文件" /> </uplevelselectortemplate> <filetemplate> <kw:FileListRemoveLink runat="server"> [x] 删除</kw:FileListRemoveLink> <kw:FileListFileName runat="server" /> <kw:FileListValidationMessage runat="server" ForeColor="Red" /> </filetemplate> <progresstemplate> <table width="100%"> <tr> <td> 文件上传中 <kw:UploadProgressElement runat="server" Element="FileCountText"> </kw:UploadProgressElement> , <kw:UploadProgressElement runat="server" Element="ContentLengthText"> (calculating)</kw:UploadProgressElement> . </td> </tr> <tr> <td> 当前正在上传: <kw:UploadProgressElement runat="server" Element="CurrentFileName"> </kw:UploadProgressElement> , 进度 <kw:UploadProgressElement runat="server" Element="CurrentFileIndex"> </kw:UploadProgressElement> / <kw:UploadProgressElement runat="server" Element="FileCount"> </kw:UploadProgressElement> . </td> </tr> <tr> <td> 上传速度: <kw:UploadProgressElement runat="server" Element="SpeedText"> (calculating)</kw:UploadProgressElement> . </td> </tr> <tr> <td> 估计剩余时间: <kw:UploadProgressElement runat="server" Element="TimeRemainingText"> (calculating)</kw:UploadProgressElement> </td> </tr> <tr> <td> <div style="border:1px solid #008800;height:1.5em;position:relative"> <kw:UploadProgressBarElement runat="server" style="background-color:#00ee00;width:0;height:1.5em"> </kw:UploadProgressBarElement> <div style="text-align:center;position:absolute;top:.15em;width:100%"> <kw:UploadProgressElement runat="server" Element="PercentCompleteText"> (calculating)</kw:UploadProgressElement> </div> </div> </td> </tr> </table> </progresstemplate> </kw:SlickUpload> <hr /> <asp:Button ID="Button1" runat="server" Text="上传" /> <asp:Button ID="cancelButton" runat="server" onclientclick="cancelUpload();return false;" Text="Cancel" style="display:none" mce_style="display:none" /> <br /> <br /> <asp:Label ID="uploadResult" runat="server"></asp:Label> <asp:Repeater ID="uploadFileList" runat="server" EnableViewState="False"> <FooterTemplate></ul></FooterTemplate> <ItemTemplate><li><%# DataBinder.Eval(Container.DataItem, "ClientName") %></li></ItemTemplate> <HeaderTemplate><ul></HeaderTemplate> </asp:Repeater> </form> </body> </html>

 

后台的代码如下:

 

view plain copy to clipboard print ?
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7.  
  8. using Krystalware.SlickUpload;//添加引用
  9. using Krystalware.SlickUpload.Status;//添加引用
  10.  
  11. public partial class SlickUploadQuickStart : System.Web.UI.Page
  12. {
  13. protected void Page_Load(object sender, EventArgs e)
  14. {
  15.  
  16. }
  17. protected void SlickUpload1_UploadComplete(object sender, Krystalware.SlickUpload.Controls.UploadStatusEventArgs e)
  18. {
  19. uploadResult.Text = "上传结果: " + e.Status.State;
  20.  
  21. if (e.Status.State == UploadState.Terminated)
  22. uploadResult.Text += ". 原因: " + e.Status.Reason;
  23.  
  24. if (e.Status.State != UploadState.Terminated)
  25. {
  26. uploadFileList.DataSource = e.UploadedFiles;
  27. uploadFileList.DataBind();
  28. }
  29. }
  30. }

using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using Krystalware.SlickUpload;//添加引用 using Krystalware.SlickUpload.Status;//添加引用 public partial class SlickUploadQuickStart : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void SlickUpload1_UploadComplete(object sender, Krystalware.SlickUpload.Controls.UploadStatusEventArgs e) { uploadResult.Text = "上传结果: " + e.Status.State; if (e.Status.State == UploadState.Terminated) uploadResult.Text += ". 原因: " + e.Status.Reason; if (e.Status.State != UploadState.Terminated) { uploadFileList.DataSource = e.UploadedFiles; uploadFileList.DataBind(); } } }

 

运行这个页面会看到如下效果:

点击上传按钮会看到如下效果:

注意,如果上传的文件超过在web.config中配置的最大允许大小会上传失败,看到的界面如下:

如果上传成功,则会看到下面的界面:


在ASP.NET AJAX中使用SlickUpload
在ASP.NET项目中新建一个ASPX页面,代码如下:

 

view plain copy to clipboard print ?
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="SlickUploadAjax.aspx.cs" Inherits="SlickUploadAjax" %>
  2. <%@ Register Assembly="Krystalware.SlickUpload" Namespace="Krystalware.SlickUpload.Controls"
  3. TagPrefix="kw" %>
  4. <%@ Import Namespace="Krystalware.SlickUpload" %>
  5. <%@ Import Namespace="Krystalware.SlickUpload.Status" %>
  6. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  7.  
  8. <html xmlns="http://www.w3.org/1999/xhtml">
  9. <head runat="server">
  10. <title>SlickUpload ASP.NET AJAX Sample</title>
  11. <mce:script type="text/javascript"><!--
  12. function startUpload()
  13. {
  14. kw.get('<%=SlickUpload1.ClientID %>').submit();
  15. }
  16.  
  17. // --></mce:script>
  18. </head>
  19. <body>
  20. <h1>SlickUpload ASP.NET AJAX Sample</h1>
  21. <form id="Form1" runat="server">
  22. <asp:ScriptManager ID="ScriptManager" runat="server" EnablePartialRendering="true">
  23. </asp:ScriptManager>
  24. <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
  25. <ContentTemplate>
  26. <div style="border:2px dotted #ccc;padding:1em;width=500px">
  27. <h2>UpdatePanel</h2>
  28. <p>时间: <asp:Label ID="updateLabel" runat="server" /></p>
  29. <p><asp:Button ID="updateButton" runat="server" Text="更新时间"
  30. onclick="updateButton_Click" /></p>
  31. <h2>选择文件</h2>
  32. <asp:Panel ID="uploadPanel" runat="server">
  33. <kw:SlickUpload ID="SlickUpload1" runat="server" AutoUploadOnPostBack="false" ShowDuringUploadElements="cancelButton" HideDuringUploadElements="uploadButton" OnUploadComplete="SlickUpload1_UploadComplete">
  34. <DownlevelSelectorTemplate>
  35. <input type="file" />
  36. </DownlevelSelectorTemplate>
  37. <UplevelSelectorTemplate>
  38. <input type="button" value="添加文件" />
  39. </UplevelSelectorTemplate>
  40. <FileTemplate>
  41. <kw:FileListRemoveLink ID="FileListRemoveLink1" runat="server">
  42. [x] 删除文件</kw:FileListRemoveLink>
  43. <kw:FileListFileName ID="FileListFileName1" runat="server" />
  44. <kw:FileListValidationMessage ID="FileListValidationMessage1" runat="server" ForeColor="Red" />
  45. </FileTemplate>
  46. <ProgressTemplate>
  47. <table width="100%">
  48. <tr>
  49. <td>
  50. 文件上传中
  51. <kw:UploadProgressElement ID="UploadProgressElement1" runat="server" Element="FileCountText">
  52. </kw:UploadProgressElement>
  53. ,
  54. <kw:UploadProgressElement ID="UploadProgressElement2" runat="server" Element="ContentLengthText">
  55. (calculating)</kw:UploadProgressElement>
  56. .
  57. </td>
  58. </tr>
  59. <tr>
  60. <td>
  61. 当前正在上传:
  62. <kw:UploadProgressElement ID="UploadProgressElement3" runat="server" Element="CurrentFileName">
  63. </kw:UploadProgressElement>
  64. , 进度
  65. <kw:UploadProgressElement ID="UploadProgressElement4" runat="server" Element="CurrentFileIndex">
  66. </kw:UploadProgressElement>
  67. /
  68. <kw:UploadProgressElement ID="UploadProgressElement5" runat="server" Element="FileCount">
  69. </kw:UploadProgressElement>
  70. .
  71. </td>
  72. </tr>
  73. <tr>
  74. <td>
  75. 上传速度:
  76. <kw:UploadProgressElement ID="UploadProgressElement6" runat="server" Element="SpeedText">
  77. (calculating)</kw:UploadProgressElement>
  78. .
  79. </td>
  80. </tr>
  81. <tr>
  82. <td>
  83. 预计剩余上传时间:
  84. <kw:UploadProgressElement ID="UploadProgressElement7" runat="server" Element="TimeRemainingText">
  85. (calculating)</kw:UploadProgressElement>
  86.  
  87. </td>
  88. </tr>
  89. <tr>
  90. <td>
  91. <div style="border: 1px solid #008800; height: 1.5em; position: relative">
  92. <kw:UploadProgressBarElement ID="UploadProgressBarElement1" runat="server" Style="background-color: #00ee00;
  93. width: 0; height: 1.5em" mce_Style="background-color: #00ee00;
  94. ">
  95. </kw:UploadProgressBarElement>
  96. <div style="text-align: center; position: absolute; top: .15em; width: 100%">
  97. <kw:UploadProgressElement ID="UploadProgressElement8" runat="server" Element="PercentCompleteText">
  98. (calculating)</kw:UploadProgressElement>
  99. </div>
  100. </div>
  101. </td>
  102. </tr>
  103. </table>
  104. </ProgressTemplate>
  105. </kw:SlickUpload>
  106. <p>
  107. <asp:Button ID="Button1" runat="server" Text="上传" OnClientClick="startUpload();return false;" />
  108. <a href="javascript:kw.get('<%=SlickUpload1.ClientID %>').cancel()" id="cancelButton" style="display:none">Cancel</a>
  109. </p>
  110. </asp:Panel>
  111. <asp:Panel ID="resultPanel" runat="server" Visible="false">
  112. <h2>上传状态</h2>
  113. <% if (SlickUpload1.UploadStatus != null) { %>
  114. <p>上传结果: <%=SlickUpload1.UploadStatus.State%>
  115. <%if (!(SlickUpload1.UploadStatus.State == UploadState.Complete || SlickUpload1.UploadStatus.State == UploadState.PostProcessingComplete))
  116. { %>
  117. <br />原因: <%=SlickUpload1.UploadStatus.Reason%>
  118. <% } %>
  119. <br />已成功上传如下文件: <%=SlickUpload1.UploadedFiles != null ? SlickUpload1.UploadedFiles.Count.ToString() : "N/A"%></p>
  120. <asp:Repeater ID="resultsRepeater" runat="server">
  121. <HeaderTemplate>
  122. <table class="results" width="99%" cellpadding="4" cellspacing="0">
  123. <thead>
  124. <tr>
  125. <th align="left">文件名</th>
  126. <th align="left">文件类型</th>
  127. <th align="left">文件大小(字节)</th>
  128. </tr>
  129. </thead>
  130. <tbody>
  131. </HeaderTemplate>
  132. <ItemTemplate>
  133. <tr>
  134. <td><%#((UploadedFile)Container.DataItem).ClientName %></td>
  135. <td><%#((UploadedFile)Container.DataItem).ContentType %></td>
  136. <td><%#((UploadedFile)Container.DataItem).ContentLength %></td>
  137. </tr>
  138. </ItemTemplate>
  139. <FooterTemplate>
  140. </tbody>
  141. </table>
  142. </FooterTemplate>
  143. </asp:Repeater>
  144. <% } %>
  145. <p><asp:Button id="newUploadButton" runat="server" Text="继续上传"
  146. onclick="newUploadButton_Click" /></p>
  147. </asp:Panel>
  148. </div>
  149. </ContentTemplate>
  150. </asp:UpdatePanel>
  151. </form>
  152. </body>
  153. </html>

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="SlickUploadAjax.aspx.cs" Inherits="SlickUploadAjax" %> <%@ Register Assembly="Krystalware.SlickUpload" Namespace="Krystalware.SlickUpload.Controls" TagPrefix="kw" %> <%@ Import Namespace="Krystalware.SlickUpload" %> <%@ Import Namespace="Krystalware.SlickUpload.Status" %> <!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 runat="server"> <title>SlickUpload ASP.NET AJAX Sample</title> <mce:script type="text/javascript"><!-- function startUpload() { kw.get('<%=SlickUpload1.ClientID %>').submit(); } // --></mce:script> </head> <body> <h1>SlickUpload ASP.NET AJAX Sample</h1> <form id="Form1" runat="server"> <asp:ScriptManager ID="ScriptManager" runat="server" EnablePartialRendering="true"> </asp:ScriptManager> <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional"> <ContentTemplate> <div style="border:2px dotted #ccc;padding:1em;width=500px"> <h2>UpdatePanel</h2> <p>时间: <asp:Label ID="updateLabel" runat="server" /></p> <p><asp:Button ID="updateButton" runat="server" Text="更新时间" /></p> <h2>选择文件</h2> <asp:Panel ID="uploadPanel" runat="server"> <kw:SlickUpload ID="SlickUpload1" runat="server" AutoUploadOnPostBack="false" ShowDuringUploadElements="cancelButton" HideDuringUploadElements="uploadButton" OnUploadComplete="SlickUpload1_UploadComplete"> <DownlevelSelectorTemplate> <input type="file" /> </DownlevelSelectorTemplate> <UplevelSelectorTemplate> <input type="button" value="添加文件" /> </UplevelSelectorTemplate> <FileTemplate> <kw:FileListRemoveLink ID="FileListRemoveLink1" runat="server"> [x] 删除文件</kw:FileListRemoveLink> <kw:FileListFileName ID="FileListFileName1" runat="server" /> <kw:FileListValidationMessage ID="FileListValidationMessage1" runat="server" ForeColor="Red" /> </FileTemplate> <ProgressTemplate> <table width="100%"> <tr> <td> 文件上传中 <kw:UploadProgressElement ID="UploadProgressElement1" runat="server" Element="FileCountText"> </kw:UploadProgressElement> , <kw:UploadProgressElement ID="UploadProgressElement2" runat="server" Element="ContentLengthText"> (calculating)</kw:UploadProgressElement> . </td> </tr> <tr> <td> 当前正在上传: <kw:UploadProgressElement ID="UploadProgressElement3" runat="server" Element="CurrentFileName"> </kw:UploadProgressElement> , 进度 <kw:UploadProgressElement ID="UploadProgressElement4" runat="server" Element="CurrentFileIndex"> </kw:UploadProgressElement> / <kw:UploadProgressElement ID="UploadProgressElement5" runat="server" Element="FileCount"> </kw:UploadProgressElement> . </td> </tr> <tr> <td> 上传速度: <kw:UploadProgressElement ID="UploadProgressElement6" runat="server" Element="SpeedText"> (calculating)</kw:UploadProgressElement> . </td> </tr> <tr> <td> 预计剩余上传时间: <kw:UploadProgressElement ID="UploadProgressElement7" runat="server" Element="TimeRemainingText"> (calculating)</kw:UploadProgressElement> </td> </tr> <tr> <td> <div style="border: 1px solid #008800; height: 1.5em; position: relative"> <kw:UploadProgressBarElement ID="UploadProgressBarElement1" runat="server" Style="background-color: #00ee00; width: 0; height: 1.5em" mce_Style="background-color: #00ee00; "> </kw:UploadProgressBarElement> <div style="text-align: center; position: absolute; top: .15em; width: 100%"> <kw:UploadProgressElement ID="UploadProgressElement8" runat="server" Element="PercentCompleteText"> (calculating)</kw:UploadProgressElement> </div> </div> </td> </tr> </table> </ProgressTemplate> </kw:SlickUpload> <p> <asp:Button ID="Button1" runat="server" Text="上传" OnClientClick="startUpload();return false;" /> <a href="javascript:kw.get('<%=SlickUpload1.ClientID %>').cancel()" id="cancelButton" style="display:none">Cancel</a> </p> </asp:Panel> <asp:Panel ID="resultPanel" runat="server" Visible="false"> <h2>上传状态</h2> <% if (SlickUpload1.UploadStatus != null) { %> <p>上传结果: <%=SlickUpload1.UploadStatus.State%> <%if (!(SlickUpload1.UploadStatus.State == UploadState.Complete || SlickUpload1.UploadStatus.State == UploadState.PostProcessingComplete)) { %> <br />原因: <%=SlickUpload1.UploadStatus.Reason%> <% } %> <br />已成功上传如下文件: <%=SlickUpload1.UploadedFiles != null ? SlickUpload1.UploadedFiles.Count.ToString() : "N/A"%></p> <asp:Repeater ID="resultsRepeater" runat="server"> <HeaderTemplate> <table class="results" width="99%" cellpadding="4" cellspacing="0"> <thead> <tr> <th align="left">文件名</th> <th align="left">文件类型</th> <th align="left">文件大小(字节)</th> </tr> </thead> <tbody> </HeaderTemplate> <ItemTemplate> <tr> <td><%#((UploadedFile)Container.DataItem).ClientName %></td> <td><%#((UploadedFile)Container.DataItem).ContentType %></td> <td><%#((UploadedFile)Container.DataItem).ContentLength %></td> </tr> </ItemTemplate> <FooterTemplate> </tbody> </table> </FooterTemplate> </asp:Repeater> <% } %> <p><asp:Button id="newUploadButton" runat="server" Text="继续上传" /></p> </asp:Panel> </div> </ContentTemplate> </asp:UpdatePanel> </form> </body> </html>

 

其对应的后台代码如下:

 

 
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7.  
  8. using Krystalware.SlickUpload.Controls;
  9.  
  10. public partial class SlickUploadAjax : System.Web.UI.Page
  11. {
  12. protected void Page_Load(object sender, EventArgs e)
  13. {
  14.  
  15. }
  16.  
  17. protected void updateButton_Click(object sender, EventArgs e)
  18. {
  19. updateLabel.Text = DateTime.Now.ToLongTimeString();
  20. }
  21.  
  22. protected void SlickUpload1_UploadComplete(object sender, UploadStatusEventArgs e)
  23. {
  24. uploadPanel.Visible = false;
  25. resultPanel.Visible = true;
  26.  
  27. if (e.UploadedFiles != null && e.UploadedFiles.Count > 0)
  28. {
  29. resultsRepeater.DataSource = e.UploadedFiles;
  30. resultsRepeater.DataBind();
  31.  
  32. resultsRepeater.Visible = true;
  33. }
  34. else
  35. {
  36. resultsRepeater.Visible = false;
  37. }
  38. }
  39.  
  40. protected void newUploadButton_Click(object sender, EventArgs e)
  41. {
  42. uploadPanel.Visible = true;
  43. resultPanel.Visible = false;
  44. }
  45. }

using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using Krystalware.SlickUpload.Controls; public partial class SlickUploadAjax : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void updateButton_Click(object sender, EventArgs e) { updateLabel.Text = DateTime.Now.ToLongTimeString(); } protected void SlickUpload1_UploadComplete(object sender, UploadStatusEventArgs e) { uploadPanel.Visible = false; resultPanel.Visible = true; if (e.UploadedFiles != null && e.UploadedFiles.Count > 0) { resultsRepeater.DataSource = e.UploadedFiles; resultsRepeater.DataBind(); resultsRepeater.Visible = true; } else { resultsRepeater.Visible = false; } } protected void newUploadButton_Click(object sender, EventArgs e) { uploadPanel.Visible = true; resultPanel.Visible = false; } }

 

运行这个页面之后,可以看到如下界面:

上传成功之后的界面如下:


说明:在上面的两个实例中演示了如何在ASP.NET中使用SlickUpload控件来上传大文件,并且分别演示了在普通的WebForm和ASP.NET AJAX中的应用。需要注意的是这个控件的默认文字是英文的,为了演示方便我将其转换成中文了,同时也为了照顾一些对ASP.NET还不熟悉的朋友直接上手感受这个控件,我会提供本文中的示例的Demo下载(可能在我上传后需要被网站审核才能下载,通过之后我会在这里附上链接,如果看不到链接请耐心等待)。
通过上面的例子,大家应该至少可以知道如何获取SlickUpload控件上传成功之后的文件信息,那就是利用控件的OnUploadComplete事件,其事件中的UploadStatusEventArgs参数有一个UploadedFiles属性,利用这个属性我们就能获取上传的文件的信息,获取这些信息之后我们就能在程序中操作这些信息。
此外由于篇幅的原因,在本篇我暂不介绍如何SlickUpload的一些比较复杂操作和属性,比如如何在客户端对要上传的文件类型进行验证、如果让上传的文件的名称按照我们期望的格式保存等等,请大家等待下一篇。
周公
2010-07-25

引用:http://blog.csdn.net/zhoufoxcn/article/details/5765748

 

你可能感兴趣的:(.net,职场,休闲,SlickUpload)