ASP.NET文件上传 [英] ASP.NET file upload

查看:99
本文介绍了ASP.NET文件上传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有网页表单,我需要上传文件。我在web.config文件中将最大文件大小设置为10mb。现在我需要验证表单中的文件上传。如果我尝试上传大于10mb的文件大小,它将转移到 HTTP错误404.13 - 未找到页面,而是我需要以与相同的形式显示错误消息最大文件具有已经延长。我为此目的使用了以下代码。



I have web form where I need to upload a file. I have set the maximum file size size as 10mb in web.config file. Now I need to validate the file upload in the form. If I try to upload a file size greater then 10mb it is moving to HTTP Error 404.13 - Not Found page but instead I need to display a error message in the same form as The maximum file has been extended . I used the following code for that purpose.

protected void btnSave_OnClick(object sender, EventArgs e)
   {
       if (uxFileUpload.FileBytes.Length > 0 )
       {
           int iMaxFileSize = 2097152;
           if (Request.ContentLength > iMaxFileSize)
           {
               exceedErrormessage.Visible=true;

               //Server.Transfer("Error.aspx");

           }







<div class="file-upload">
        <asp:Label ID="uiFileUpload" runat="server" AssociatedControlID="uxFileUpload" Text="Select file" />
            <p>The maximun file size is 20mb</p>
        <asp:FileUpload ID="uxFileUpload" runat="server" />
            <asp:PlaceHolder ID="exceedErrormessage" Visible="false" runat="server">
                 <asp:Literal ID="exServerErroemessage" runat="server" Text="The maximum file limit has been exceeded" />
    </asp:PlaceHolder>
            <%--<asp:Label ID="label" runat="server"></asp:Label>--%>
            <asp:CustomValidator ID="CustomValidator1" runat="server" ControlToValidate="uxFileUpload"

   ErrorMessage="File size should not be greater than 20 mb." OnServerValidate="btnSave_OnClick"></asp:CustomValidator>
        <asp:Button ID="btnSaveDocument" ValidationGroup="SaveFile" OnClick="btnSave_OnClick" runat="server" Text="Upload" CssClass="button" />
    </div>





但是当我尝试上传大于10mb的文件大小时,它仍然会移动到相同的错误页面。我甚至尝试放置一个断点并检查问题发生的位置。我发现当我尝试上传大于10 mb的文件大小时,它甚至没有移动到 btnSave_OnClick 函数,但是如果我上传的文件大小小于10 mb,它将转移到该函数并且已执行相应的操作。任何人都可以帮我解决这个问题。



But still it is moving to the same error page when I try to upload a file size greater then 10mb. I even tried placing a breakpoint and checked where the problem is occurring. I found that when I try to upload a file size greater than 10 mb it is not even moving to the btnSave_OnClick function but otherwise if I upload a file size less than 10 mb it is moving to the function and the corresponding operation is been performed. Can anyone help me to solve this problem.

推荐答案

你所取的值2097152是以字节为单位,仅等于2mb而不是20mb。所以请先将其更改为适当的值,即20971520
Hi the value you have taken 2097152 is in bytes which is equals to 2mb only but not 20mb. so please change this first to appropriate value which is 20971520


这篇关于ASP.NET文件上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆