title: Asp.Net Ajax Toolkit AsyncFileUpload - "The file is attached is invalid" error
body: <p>I'm trying to use the AsyncFileUpload control from the Asp.net Ajax Control Toolkit (Sept 30, 2009 stable build: 30930) . I've created a demo application and the control works fine. Files upload and all is well. </p>
  
  <p>When I try to use the control in my real application, I'm always receiving an error stating "The file attached is invalid". The AsyncFileUpload control returns this when the file uploaded is null. </p>
  
  <p>To isolate the problem, I created a new master page exactly like the master page in my demo app. I also created an aspx page exactly like the page in my demo app. The upload still fails with a "The file attached is invalid" error.</p>
  
  <p>I also compared the web.config for the real app and the demo app and couldn't identify any differences that should matter.</p>
  
  <p>The code below is the test code in my real app. This code is exactly the same as the functioning code in the demo app with the exception of having different class names and file names. The web.config listed below is from the real app with the appsettings and connectionstrings removed.</p>
  
  <p>At this point, I am completely stumped. </p>
  
  <p>Real App Test Master Page:</p>
  
  <pre><code>&lt;%@ Master Language="C#" AutoEventWireup="true" CodeFile="aaaMasterPage.master.cs" Inherits="L1Admin.aaaMasterPage" %&gt;
  
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
  
  &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
  &lt;head runat="server"&gt;
      &lt;title&gt;Untitled Page&lt;/title&gt;
      &lt;script type="text/javascript" src="js/jquery-1.3.2.min.js"&gt;&lt;/script&gt;    
      &lt;asp:ContentPlaceHolder id="head" runat="server"&gt;
      &lt;/asp:ContentPlaceHolder&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;form id="form1" runat="server"&gt;
      &lt;asp:ScriptManager ID="scriptManager" EnablePartialRendering="true" runat="server" AsyncPostBackTimeout="180" /&gt;    
      &lt;div&gt;
          &lt;asp:ContentPlaceHolder id="content" runat="server"&gt;
  
          &lt;/asp:ContentPlaceHolder&gt;
      &lt;/div&gt;
      &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Test Page:</p>
  
  <pre><code>&lt;%@ Page Language="C#" MasterPageFile="~/aaaMasterPage.master" AutoEventWireup="true" CodeFile="aaaFileUploadTest2.aspx.cs" Inherits="aaaFileUploadTest2" Title="Untitled Page" %&gt;
  &lt;%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajax" %&gt;
  
      &lt;asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server"&gt;
          &lt;script type="text/javascript"&gt;
              function pageLoad(sender, args) {
              }
  
              function startUpload(sender,args)
              {
                  $('#uploadMessage p').html();
                  $('#uploadMessage').hide();
              }
  
              function uploadComplete(sender,args)
              {
                  showUploadMessage(args.get_fileName() + " uploaded succesfully - " +  + args.get_length() + " bytes", '');
              }
  
              function uploadError(sender, args)
              {
                  showUploadMessage("An error occurred during uploading. " + args.get_errorMessage(), '#ff0000');
              }
  
              function showUploadMessage(text, color)
              {
                  $('#uploadMessage p').html(text).css('color', color);
                  $('#uploadMessage').show();
              }
          &lt;/script&gt;
      &lt;/asp:Content&gt;
      &lt;asp:Content ID="Content2" ContentPlaceHolderID="content" Runat="Server"&gt;    
          &lt;div id="uploadMessage"&gt;&lt;p&gt;&lt;/p&gt;&lt;/div&gt;
          &lt;ajax:AsyncFileUpload ID="pageBannerUpload"
              CssClass="file_upload"
              OnClientUploadError="uploadError"
              OnClientUploadStarted="startUpload"
              OnClientUploadComplete="uploadComplete"
              onuploadedcomplete="upload_UploadedComplete"
              runat="server" /&gt;
      &lt;/asp:Content&gt;
  </code></pre>
  
  <p>Real App Test Page Code Behind:</p>
  
  <pre><code>using System;
  using System.IO;
  using System.Collections;
  using System.Configuration;
  using System.Data;
  using System.Linq;
  using System.Web;
  using System.Web.Security;
  using System.Web.UI;
  using System.Web.UI.HtmlControls;
  using System.Web.UI.WebControls;
  using System.Web.UI.WebControls.WebParts;
  using System.Xml.Linq;
  
  public partial class aaaFileUploadTest2 : System.Web.UI.Page
  {
      protected void Page_Load(object sender, EventArgs e)
      {
  
      }
  
      protected void upload_UploadedComplete(object sender, AjaxControlToolkit.AsyncFileUploadEventArgs e)
      {
          if (pageBannerUpload.HasFile)
          {
              string path = MapPath("~/") + Path.GetFileName(e.filename);
              pageBannerUpload.SaveAs(path);
          }
      }
  }
  </code></pre>
  
  <p>Real App Test Web.config:</p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;!-- 
      Note: As an alternative to hand editing this file you can use the 
      web admin tool to configure settings for your application. Use
      the Website-&gt;Asp.Net Configuration option in Visual Studio.
      A full list of settings and comments can be found in 
      machine.config.comments usually located in 
      \Windows\Microsoft.Net\Framework\v2.x\Config 
  --&gt;
  &lt;configuration&gt;
      &lt;configSections&gt;
          &lt;sectionGroup name="system.web.extensions" type="System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"&gt;
              &lt;sectionGroup name="scripting" type="System.Web.Configuration.ScriptingSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"&gt;
                  &lt;section name="scriptResourceHandler" type="System.Web.Configuration.ScriptingScriptResourceHandlerSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/&gt;
                  &lt;sectionGroup name="webServices" type="System.Web.Configuration.ScriptingWebServicesSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"&gt;
                      &lt;section name="jsonSerialization" type="System.Web.Configuration.ScriptingJsonSerializationSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="Everywhere"/&gt;
                      &lt;section name="profileService" type="System.Web.Configuration.ScriptingProfileServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/&gt;
                      &lt;section name="authenticationService" type="System.Web.Configuration.ScriptingAuthenticationServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/&gt;
                      &lt;section name="roleService" type="System.Web.Configuration.ScriptingRoleServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/&gt;
                  &lt;/sectionGroup&gt;
              &lt;/sectionGroup&gt;
          &lt;/sectionGroup&gt;
      &lt;/configSections&gt;
      &lt;appSettings&gt;
  
      &lt;/appSettings&gt;
    &lt;connectionStrings&gt;
    &lt;/connectionStrings&gt;
      &lt;system.web&gt;
          &lt;!-- 
              Set compilation debug="true" to insert debugging 
              symbols into the compiled page. Because this 
              affects performance, set this value to true only 
              during development.
          --&gt;
          &lt;compilation debug="true"&gt;
              &lt;assemblies&gt;
                  &lt;add assembly="System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/&gt;
                  &lt;add assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/&gt;
                  &lt;add assembly="System.Xml.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/&gt;
                  &lt;add assembly="System.Data.DataSetExtensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/&gt;
                  &lt;add assembly="System.Transactions, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/&gt;
                  &lt;add assembly="System.Data.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/&gt;
              &lt;/assemblies&gt;
              &lt;codeSubDirectories&gt;
                  &lt;add directoryName="CSCode"/&gt;
                  &lt;add directoryName="VBCode"/&gt;
              &lt;/codeSubDirectories&gt;
          &lt;/compilation&gt;
          &lt;!--
              The &lt;authentication&gt; section enables configuration 
              of the security authentication mode used by 
              ASP.NET to identify an incoming user. 
          --&gt;
          &lt;authentication mode="Windows"/&gt;
          &lt;!--
              The &lt;customErrors&gt; section enables configuration 
              of what to do if/when an unhandled error occurs 
              during the execution of a request. Specifically, 
              it enables developers to configure html error pages 
              to be displayed in place of a error stack trace.
  
          &lt;customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm"&gt;
              &lt;error statusCode="403" redirect="NoAccess.htm" /&gt;
              &lt;error statusCode="404" redirect="FileNotFound.htm" /&gt;
          &lt;/customErrors&gt;
          --&gt;
          &lt;pages&gt;
              &lt;controls&gt;
                  &lt;add tagPrefix="asp" namespace="System.Web.UI" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/&gt;
                  &lt;add tagPrefix="asp" namespace="System.Web.UI.WebControls" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/&gt;
                  &lt;add tagPrefix="Annsa" namespace="Annsa.Controls" assembly="Annsa.Controls"/&gt;
              &lt;/controls&gt;
              &lt;namespaces&gt;
                  &lt;add namespace="Microsoft.VisualBasic"/&gt;
                  &lt;add namespace="System.Data"/&gt;
                  &lt;add namespace="System.Drawing"/&gt;
              &lt;/namespaces&gt;
          &lt;/pages&gt;
          &lt;httpHandlers&gt;
              &lt;remove verb="*" path="*.asmx"/&gt;
              &lt;add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/&gt;
              &lt;add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/&gt;
              &lt;add verb="GET,HEAD" path="ScriptResource.axd" validate="false" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/&gt;
          &lt;/httpHandlers&gt;
          &lt;httpModules&gt;
              &lt;add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/&gt;
          &lt;/httpModules&gt;
          &lt;identity impersonate="true"/&gt;
          &lt;trace enabled="false" requestLimit="10" pageOutput="false" traceMode="SortByTime" localOnly="true"/&gt;
          &lt;!--  SESSION STATE SETTINGS
              mode="Off|InProc|StateServer|SqlServer"
            By default ASP.NET uses cookies to identify which requests belong to a particular session. 
            If cookies are not available, a session can be tracked by adding a session identifier to the URL. 
            To disable cookies, set sessionState cookieless="true".
          --&gt;
          &lt;sessionState mode="InProc" stateConnectionString="tcpip=127.0.0.1:42424" sqlConnectionString="data source=127.0.0.1;user id=sa;password=" cookieless="false" timeout="20"/&gt;
          &lt;!--  GLOBALIZATION
            This section sets the globalization settings of the application. 
          --&gt;
          &lt;xhtmlConformance mode="Legacy"/&gt;
      &lt;/system.web&gt;
    &lt;location path="Pages"&gt;
      &lt;system.web&gt;
        &lt;xhtmlConformance mode="Transitional"&gt;&lt;/xhtmlConformance&gt;
      &lt;/system.web&gt;
    &lt;/location&gt;
      &lt;system.codedom&gt;
          &lt;compilers&gt;
              &lt;compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CSharp.CSharpCodeProvider,System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" warningLevel="4"&gt;
                  &lt;providerOption name="CompilerVersion" value="v3.5"/&gt;
                  &lt;providerOption name="WarnAsError" value="false"/&gt;
              &lt;/compiler&gt;
              &lt;compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.VisualBasic.VBCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" warningLevel="4"&gt;
                  &lt;providerOption name="CompilerVersion" value="v3.5"/&gt;
                  &lt;providerOption name="OptionInfer" value="true"/&gt;
                  &lt;providerOption name="WarnAsError" value="false"/&gt;
              &lt;/compiler&gt;
          &lt;/compilers&gt;
      &lt;/system.codedom&gt;
      &lt;system.webServer&gt;
          &lt;validation validateIntegratedModeConfiguration="false"/&gt;
          &lt;modules&gt;
              &lt;remove name="ScriptModule"/&gt;
              &lt;add name="ScriptModule" preCondition="managedHandler" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/&gt;
          &lt;/modules&gt;
          &lt;handlers&gt;
              &lt;remove name="WebServiceHandlerFactory-Integrated"/&gt;
              &lt;remove name="ScriptHandlerFactory"/&gt;
              &lt;remove name="ScriptHandlerFactoryAppServices"/&gt;
              &lt;remove name="ScriptResource"/&gt;
              &lt;add name="ScriptHandlerFactory" verb="*" path="*.asmx" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/&gt;
              &lt;add name="ScriptHandlerFactoryAppServices" verb="*" path="*_AppService.axd" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/&gt;
              &lt;add name="ScriptResource" verb="GET,HEAD" path="ScriptResource.axd" preCondition="integratedMode" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/&gt;
          &lt;/handlers&gt;
      &lt;/system.webServer&gt;
      &lt;runtime&gt;
          &lt;assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"&gt;
              &lt;dependentAssembly&gt;
                  &lt;assemblyIdentity name="System.Web.Extensions" publicKeyToken="31bf3856ad364e35"/&gt;
                  &lt;bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="3.5.0.0"/&gt;
              &lt;/dependentAssembly&gt;
              &lt;dependentAssembly&gt;
                  &lt;assemblyIdentity name="System.Web.Extensions.Design" publicKeyToken="31bf3856ad364e35"/&gt;
                  &lt;bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="3.5.0.0"/&gt;
              &lt;/dependentAssembly&gt;
          &lt;/assemblyBinding&gt;
      &lt;/runtime&gt;
  &lt;/configuration&gt;
  </code></pre>
  
  <p>Edit: I'm no longer working for this company, so I can't test any new answers to see if they fix the problem.</p>