4

I've been using the FileUpload control to upload a large file. Everything works fine on localhost, but on the server, the file gets corrupt: the upload goes well, the file has correct size, but the contents are mixed up, some parts of the file are written over others.

E.g. like this:

Original file:
abcdef0123456789

Uploaded file:
abc1230123456789

There is about one segment like this per 10 MBs of the file, the length varies, typically 0.1-1 kB.

My code is simple:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="UploadTest.Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:FileUpload ID="FileUpload1" runat="server" />

    </div>
    <p>
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Submit" />
    </p>
    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    </form>
</body>
</html>

And the server side:

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace UploadTest
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (FileUpload1.HasFile) 
            {
                FileUpload1.SaveAs(AppDomain.CurrentDomain.BaseDirectory + "\\Files\\" + FileUpload1.FileName);
                Label1.Text = "Success " + FileUpload1.FileName;
            }
            else {
                Label1.Text = "No file";
            }
        }
    }
}

It does not matter what file I try to upload, the large ones (> 10 MB) get corrupted.

I think that this has no business with the code, but it is related to something else (but it does not happen on my client computer only and I need solution simple enough to explain it to the other person using it, if this is client-related). I've tried to disable the antivirus software and it had no effect. But also, there are some clients that handle the data upload correctly.

What else can mess up my post data?

Any ideas? Thanks, AlesP

Post edited

alesp
  • 49
  • 1
  • 3
  • I've had odd issues with anti-virus programmings messing up post data, try disabling yours if you have one enabled. – Matthew Jun 18 '12 at 13:56
  • if you would edit question to follow up Skeets mind flow (http://msmvps.com/blogs/jon_skeet/archive/2012/03/16/diagnosing-weird-problems-a-stack-overflow-case-study.aspx) would be easier to help (I mean piece of code and sample file to reproduce case by our selves would help) – Giedrius Jun 18 '12 at 14:00
  • Until you provide the code we cannot help you. What I cannot explain is the reason a vague question like this was voted up so many times. The last sentence for example makes no sense. – Security Hound Jun 18 '12 at 14:26
  • Check http://msdn.microsoft.com/en-us/library/e1f13641%28v=vs.71%29.aspx – Pabuc Jun 18 '12 at 14:37
  • Thanks for replies, antivirus software was not the cause, unfortunately. I've added the code, but I've simplified it prior asking the question so I don't think that this is the cause. @Pabuc, I've set the maximum request length (earlier), have you meant anything else? – alesp Jun 18 '12 at 15:29
  • Well I am not sure, but it could be happening because of bad internet connection, data packet losses and bad pings. – Ashwin Singh Jun 25 '12 at 06:30

2 Answers2

0

Change your <form> encoding attribute to enctype="multipart/form-data".

Dai
  • 141,631
  • 28
  • 261
  • 374
0

It seems that you may be exceeding the default file size limit, which is a setting I think you can change. See http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.fileupload.aspx

Specifically the section on memory limitations.

Edit

This looks like it may help you: Large POST data is corrupted when using Django/PyISAPIe/IIS

Community
  • 1
  • 1
Andrew Hagner
  • 804
  • 1
  • 9
  • 18
  • I have set the limit to 500 MB during the initial development - the file would not be uploaded at all, would it? – alesp Jun 18 '12 at 15:44
  • True, I just looked at you code now and it looks correct, it is very odd that this is happening. Maybe you are hitting the web-browser's limit instead? But again it probably wouldn't work at all then. – Andrew Hagner Jun 18 '12 at 16:41
  • Thanks. After some attempts with different computers, it seems that this happens on one client computer only. Strange, but it is probably caused by some paranoid antivirus on that device. – alesp Jun 25 '12 at 07:41