Asp.net文件上传问题"算术运算导致溢出&QUOT。 CONTENTLENGTH总是-2 [英] Asp.net FileUpload problem "Arithmetic operation resulted in an overflow." ContentLength is always -2

查看:279
本文介绍了Asp.net文件上传问题"算术运算导致溢出&QUOT。 CONTENTLENGTH总是-2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好我有一个在我的测试解决方案的工作可爱的更新面板,但是当我把它变成主要的项目就不能正常工作。
我已经说得很简单,但仍然没有喜悦,它包括:


  1. 文件上传控件

  2. 的链接按钮

链接按钮有一个onclick方法,它采用的文件,并创建一个字节数组。出于某种原因,CONTENTLENGTH为-2每次。不要紧,我用的是什么类型的文件。每次!
这是非常令人沮丧,考虑到它的作品在我的测试解决方案,很好。

有什么我失踪或者应该看什么?

感谢:)

编辑:

我使用VS2008

code:

 < HTML的xmlns =htt​​p://www.w3.org/1999/xhtml>
<头ID =头像1=服务器>
< /头>
<身体GT;
    <表ID =form1的=服务器>
        < ASP:文件上传ID =FileUpload1=服务器/>
        < ASP:LinkBut​​ton的ID =btnUpload=服务器的ValidationGroup =uploadform的CssClass =uploadbutton的OnClick =btnUpload_Click>&上传LT; / ASP:LinkBut​​ton的>
    < /表及GT;
< /身体GT;
< / HTML>

C#

 保护无效btnUpload_Click(对象发件人,EventArgs的发送)
{
    VAR intDoccumentLength = FileUpload1.PostedFile.ContentLength;    //这里将作为崩溃内容长度为-2由于某种原因???〜
    字节[] =新建文档新的字节[intDoccumentLength]
}


解决方案

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

 <%@页面语言=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>

这篇关于Asp.net文件上传问题&QUOT;算术运算导致溢出&QUOT。 CONTENTLENGTH总是-2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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