且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Asp.net文件上传问题"算术运算导致溢出&QUOT。 CONTENTLENGTH总是-2

更新时间:2023-02-15 22:15:15

该文件引导加载器并非如此,如果你正在使用的更新面板在更新面板运行良好,那么你必须使用触发器在更新面板,你必须给控制或按钮的上你点击上载的文件名

 <%@页面语言=C#的MasterPageFile =〜/ FullViewMasterPage.masterAutoEventWireup =真codeFILE =Test1.aspx.cs继承= Test1的标题=无标题页%GT;
<%@注册标签preFIX =YAF命名空间=YAF大会=YAF%GT;
<%@注册标签preFIX =YC命名空间=YAF.Controls大会=YAF%GT;
< ASP:内容ID =内容1ContentPlaceHolderID =FullViewContentPlaceHolder=服务器>
   <表ID =form1的=服务器>
< ASP:的ScriptManager ID =ScriptManager1=服务器/>< ASP:的UpdatePanel ID =UpdatePanel1=服务器><&的ContentTemplate GT;
< ASP:文件上传ID =文件上传=服务器>< / ASP:文件上传>
< ASP:按钮的ID =上传=服务器的OnClick =Upload_Click文本=上传图片/>< BR />
< /&的ContentTemplate GT;
< / ASP:的UpdatePanel>
< BR /><&触发器GT;
< ASP:AsyncPostBackTrigger控件ID =上传事件名称=Upload_Click/>
< /触发器>< ASP:的UpdatePanel ID =UpdatePanel2=服务器><&的ContentTemplate GT;
< ASP:标签ID =lblTime3=服务器/>< BR />
< /&的ContentTemplate GT;
< / ASP:的UpdatePanel>
< /表及GT;< / ASP:内容>
< code>

Hi all I have an update panel 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];
}

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>