• ASP.NET 大文件上传的简单处理


    在 ASP.NET 开发的过程中,文件上传往往使用自带的 FileUpload 控件,可是用过的人都知道,这个控件的局限性十分大,最大的问题就在于上传大文件时让开发者尤为的头疼,而且,上传时无法方便的做到多线程的操控和上传进度的显示。在此给大家推荐一款简单易用的上传组件,从而快速便捷得解决了 ASP.NET 中的大文件上传问题。

    首先,我们需要这个名为 RanUpLoad 的组件(下面例子中附带),这两个 dll 文件添加到项目的引用中区,xml 文件也要复制在项目中的 bin 文件夹下,也就是最后三个文件都要存在于 bin 文件夹中。

    接着,上传控件还是用 ASP.NET 中自带的 FileUpload 控件,需要添加的就是在 FileUpload 控件旁边加入标签:

    C#代码  收藏代码
    1. <radU:RadProgressManager ID="Radprogressmanager1" Width="100%" runat="server" />  
    2. <radU:RadProgressArea ID="progressArea1" Width="100%" runat="server"></radU:RadProgressArea>  

     并且在 前台页面aspx 文件的起始处添加如下代码:

    C#代码  收藏代码
    1. <form id="form1" runat="server">  
    2.     <div>  
    3.         <radU:RadProgressManager ID="Radprogressmanager1" Width="100%" runat="server" />  
    4.         <radU:RadProgressArea ID="progressArea1" Width="100%" runat="server">  
    5.         </radU:RadProgressArea>  
    6.         <asp:FileUpload runat="server" ID="fileUPload" />  
    7.         &nbsp;&nbsp;&nbsp;  
    8.         <asp:Button runat="server" ID="fileUpladBtn" Text="上传" OnClick="fileUpladBtn_OnClick" />  
    9.     </div>  
    10.     </form>  

     当然,配置文件的 <system.web> 标签中不能忘记下面这些语句:

    C#代码  收藏代码
    1. <!--文件上传控件配置-->  
    2.    <httpRuntime executionTimeout="3600" maxRequestLength="2097151" ></httpRuntime>  
    3.    <httpModules>  
    4.      <add name="RadUploadModule" type="Telerik.WebControls.RadUploadHttpModule, RadUpload.Net2"/>  
    5.    </httpModules>  
    6.    <httpHandlers>  
    7.      <add verb="*" path="Telerik.RadUploadProgressHandler.aspx" type="Telerik.WebControls.RadUploadProgressHandler, RadUpload.Net2"></add>  
    8.    </httpHandlers>  

     下面就是后台文件上传操作

    C#代码  收藏代码
    1. protected void fileUpladBtn_OnClick(object sender, EventArgs e)  
    2.         {  
    3.             if (RadUploadContext.Current == null) { return; }  
    4.             if (RadUploadContext.Current.UploadedFiles.Count <= 0)  
    5.             {  
    6.                 this.Page.ClientScript.RegisterStartupScript(this.Page.GetType(), "MsgBox", "<script>alert('请选择上传文件 !')</script>");  
    7.                 return;  
    8.             }  
    9.             if (RadUploadContext.Current.UploadedFiles[0].ContentLength >= 2147483647)  
    10.             {  
    11.                 this.Page.ClientScript.RegisterStartupScript(this.Page.GetType(), "MsgBox", "<script>alert('上传的文件不得超过 2GB !')</script>");  
    12.                 return;  
    13.             }  
    14.             UploadedFile file = RadUploadContext.Current.UploadedFiles[0];  
    15.             string fileName = Path.GetFileName(file.FileName);  
    16.             string virtualPath = System.IO.Path.Combine("~/Files", fileName);  
    17.             string savePath = this.MapPath(virtualPath);  
    18.             file.SaveAs(savePath, true);   
    19.         }  
  • 相关阅读:
    Forms身份验证
    常见的js图片或内轮换效果
    模仿select选择框
    基于jquery的js幻灯片类
    js弹出幕布遮罩层
    css那些事
    选择珠宝js业务逻辑源码
    aspnet帐号密码改了会出问题
    用rails做了个书评排行网站,欢迎光临!
    转发:为什么函数式编程至关重要
  • 原文地址:https://www.cnblogs.com/ranran/p/3848746.html
Copyright © 2020-2023  润新知