开发者

Asp.net FileUpload problem "Arithmetic operation resulted in an overflow." ContentLength is always -2

开发者 https://www.devze.com 2023-02-21 19:01 出处:网络
Hi all I have an update pa开发者_如何学Gonel that works lovely in my test solution but when I put it into the main project it does not work correctly.

Hi all I have an update pa开发者_如何学Gonel that works lovely in my test solution but when I put it into the main project it does not work correctly. I've made it very simple, but still no joy, it consists of:

  1. the file upload control
  2. a link button

the link button has an onclick method that takes the file and creates a byte array. For some reason the contentLength is -2 every time. It does not matter what type of file I am using. Every time! This is very frustrating considering it works fine in my test solution.

Is there anything I am missing or should be looking at?

Thanks :)

EDIT:

I am using VS2008

CODE:

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
</head>
<body>
    <form id="form1" runat="server">    
        <asp:FileUpload ID="FileUpload1" runat="server"  />  
        <asp:LinkButton ID="btnUpload" runat="server" ValidationGroup="uploadform" CssClass="uploadbutton" OnClick="btnUpload_Click">Upload</asp:LinkButton>      
    </form>
</body>
</html>

C#

protected void btnUpload_Click(object sender, EventArgs e)
{
    var intDoccumentLength = FileUpload1.PostedFile.ContentLength;

    // will crash here as content length is -2 for some reason~???
    byte[] newDocument = new byte[intDoccumentLength];
}


Your test code must be doing complete postback which is not the case for Updatepanel part as part of main project. With updatepanel, file may not have been uploaded at point of time when you are checking its contentlenght; this is not true for complete postback where file always gets uploaded first. In this case, it will always through you an error. This can only be done using an ActiveX control of some kind.

This article may give you nice hint and direction: asp.net FileUpload event after choice is made and before submit for upload


The file up loader is not working well in the update panel so if you are using the update panel then you have to use the triggers in the update panel and you have to give the name of the control or button on which you are clicking to upload the file

    <%@ Page Language="C#" MasterPageFile="~/FullViewMasterPage.master" AutoEventWireup="true" CodeFile="Test1.aspx.cs" Inherits="Test1" Title="Untitled Page" %>
<%@ Register TagPrefix="yaf" Namespace="YAF" Assembly="YAF" %>
<%@ Register TagPrefix="yc" Namespace="YAF.Controls" Assembly="YAF" %>
<asp:Content ID="Content1" ContentPlaceHolderID="FullViewContentPlaceHolder" Runat="Server">


   <form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" />

<asp:UpdatePanel ID="UpdatePanel1" runat="server">

<ContentTemplate>
<asp:FileUpload id="fileUpload" runat="server" ></asp:FileUpload>
<asp:Button ID="Upload" runat="server" OnClick="Upload_Click"  Text="Upload The Image" /><br />
</ContentTemplate>
</asp:UpdatePanel>
<br />

<Triggers>
<asp:AsyncPostBackTrigger ControlID="Upload" EventName="Upload_Click" />
</Triggers>

<asp:UpdatePanel ID="UpdatePanel2" runat="server">

<ContentTemplate>
<asp:Label ID="lblTime3" runat="server" /><br />
</ContentTemplate>
</asp:UpdatePanel>
</form>

</asp:Content>
<code>


As FileUpload.PostedFile.InputStream.Length returns the same as FileUpload.PostedFile.ContentLength, did you check that your file size is lower than maxRequestLength (4MB by default) or specified in web.config :

<system.web>
    <httpRuntime maxRequestLength="8192"/>
</system.web>

See : http://msdn.microsoft.com/en-us/library/system.web.configuration.httpruntimesection.maxrequestlength.aspx

0

精彩评论

暂无评论...
验证码 换一张
取 消