asp.net大文件上傳

下載neatupload的免費版本 http://neatupload.codeplex.com/javascript

訪問地址http://www.brettle.com/Demo.aspx查看相應的Demo示例;
 css

將下載下來的文件解壓之後,將Brettle.Web.NeatUpload.dll添加到工具箱,html

將裏面的NeatUpload文件夾複製到項目根目錄,接着修改web.config的配置文件java

<?xml version="1.0" encoding="utf-8"?>web

<configuration>服務器

    <configSections>app

        <sectionGroup name="system.web">ide

            <section name="neatUpload" type="Brettle.Web.NeatUpload.ConfigSectionHandler, Brettle.Web.NeatUpload" allowLocation="true" />工具

        </sectionGroup>字體

    </configSections>

 

    <appSettings/>

    <connectionStrings/>

 

    <system.web>

        <neatUpload useHttpModule="false" maxNormalRequestLength="4096" maxRequestLength="2097151" defaultProvider="FilesystemUploadStorageProvider">

            <providers>

                <add name="FilesystemUploadStorageProvider"

                type="Brettle.Web.NeatUpload.FilesystemUploadStorageProvider, Brettle.Web.NeatUpload" />

            </providers>

        </neatUpload>

        <compilation debug="false">

        </compilation>

        <authentication mode="Windows" />

        <httpModules>

            <!--若是不加這httpmodules,進度條不顯示-->

           <add name="UploadHttpModule" type="Brettle.Web.NeatUpload.UploadHttpModule, Brettle.Web.NeatUpload"/>

        </httpModules>

    </system.web>

    <location path="Default.aspx" ><!--Default頁面的上傳,已經上傳的大小限制-->

        <system.web>

            <neatUpload useHttpModule="true" />

            <httpRuntime maxRequestLength="2097151" executionTimeout="3600" useFullyQualifiedRedirectUrl="true" />

        </system.web>

    </location>

</configuration>

接着開始往窗體上拖放控件,以下圖操做

 

 

.aspx頁面代碼以下:

<head runat="server">

    <title> NeatUpload </title>

    <script type="text/javascript" language="javascript">

       function ToggleVisibility(id, type)

       {

           el = document.getElementById(id);

           if(el.style)

           {

               if(type == 'on')

               { 

                  el.style.display = 'block';

               }

               else

               { 

                  el.style.display = 'none';

               }

           }

           else

           {

               if(type == 'on')

               { 

                   el.display = 'block';

               }

               else

               {      

                  el.display = 'none';

               }

           }

       }

   </script>

</head>

<body>

    <form id="form1" runat="server">

    <div>

        <Upload:InputFile ID="AttachFile" runat="server" />

   <asp:Button ID="Upload" runat="server" Text="Upload"

            OnClientClick="ToggleVisibility('ProgressBar','on')" onclick="Upload_Click" />

       <div id="ProgressBar" >

       <Upload:ProgressBar ID="pbProgressBar" runat='server' Inline="true" Width="280px"

               Height="50px">

       </Upload:ProgressBar>

   </div>

   </div>

    </form>

</body>

</html>

設計界面以下圖顯示:

 

 

接下來,開始寫上傳的文件執行的操做;.cs代碼以下:

protected void Upload_Click(object sender, EventArgs e)

        {

            string FileName = this.AttachFile.FileName;//獲取上傳文件的全路徑

            string ExtenName = System.IO.Path.GetExtension(FileName);//獲取擴展名

            string SaveFileName = System.IO.Path.Combine(Request.PhysicalApplicationPath, DateTime.Now.ToString("yyyyMMddhhmm") + ExtenName);//合併兩個路徑爲上傳到服務器上的全路徑

            if (this.AttachFile.ContentLength > 0)

            {

                try

                {

                    this.AttachFile.MoveTo(SaveFileName, Brettle.Web.NeatUpload.MoveToOptions.Overwrite);

                }

                catch (Exception ex)

                {

                    throw ex;

                }

            }

        }

作到這裏程序就能夠運行了,默認的進度條樣式是能夠修改的。

主要修改default.css、Progress.aspx及ProgressBar控件的屬性。

 

 

default.css修改部分:

.ProgressDisplay .ProgressBar {

      background-color: #D7E5F7;

      background-p_w_picpath: url("/11.gif")/*進度條背景圖片*/

      height:20px;

}

#normalInProgress{

     color: Red;      /*進度條字體顏色*/

}

#completed{

     color:Blue;   /*上傳完成的字體顏色*/

}

 

Progress.aspx是顯示進度條的核心文件,相關數據的顯示都在裏面。

<td id="barTd" >

              <div id="statusDiv" runat="server" class="StatusMessage">

 

                   <Upload:DetailsSpan id="normalInProgress" runat="server" WhenStatus="NormalInProgress" style="font-weight: normal; white-space: nowrap;">                                <%--  正在上傳時顯示的   --%>

                   <%-- <%# FormatCount(BytesRead) %>/<%# FormatCount(BytesTotal) %> <%# CountUnits %> --%>

                  (<%# String.Format("{0:0%}", FractionComplete) %>) at <%# FormatRate(BytesPerSec) %>

                   <%-- - <%# FormatTimeSpan(TimeRemaining) %> left        --%>

                   </Upload:DetailsSpan>

 

                   <Upload:DetailsSpan id="chunkedInProgress" runat="server" WhenStatus="ChunkedInProgress" style="font-weight: normal; white-space: nowrap;">       

                    <%# FormatCount(BytesRead) %> <%# CountUnits %>

                       at <%# FormatRate(BytesPerSec) %>

                       - <%# FormatTimeSpan(TimeElapsed) %> elapsed

                   </Upload:DetailsSpan>

                   <Upload:DetailsSpan id="completed" runat="server" WhenStatus="Completed">

                       <%--  上傳完成時顯示的   --%>

完成了: <%# FormatCount(BytesRead) %> <%# CountUnits %>

                       at <%# FormatRate(BytesPerSec) %>

                       took <%# FormatTimeSpan(TimeElapsed) %>

                   </Upload:DetailsSpan>

                   <Upload:DetailsSpan id="cancelled" runat="server" WhenStatus="Cancelled">

                       Cancelled!

                   </Upload:DetailsSpan>

                   <Upload:DetailsSpan id="rejected" runat="server" WhenStatus="Rejected">

                       Rejected: <%# Rejection != null ? Rejection.Message : "" %>

                   </Upload:DetailsSpan>

                   <Upload:DetailsSpan id="error" runat="server" WhenStatus="Failed">

                       Error: <%# Failure != null ? Failure.Message : "" %>

                   </Upload:DetailsSpan>

                   <Upload:DetailsDiv id="barDetailsDiv" runat="server" UseHtml4="true"

                        Width='<%# Unit.Percentage(Math.Floor(100*FractionComplete)) %>' class="ProgressBar"></Upload:DetailsDiv> 

              </div>

         </td>

 

程序的效果圖以下:

上傳前:

 

上傳中:

 

上傳結束

轉載自:http://www.cnblogs.com/langlang/archive/2009/12/11/1621730.html

相關文章
相關標籤/搜索