Asp.net

Asp.Net Ajax Toolkit AsyncFileUpload - “附加的文件無效”錯誤

  • June 2, 2015

我正在嘗試使用 Asp.net Ajax Control Toolkit (Sept 30, 2009 stable build: 30930) 中的 AsyncFileUpload 控制項。我創建了一個展示應用程序,並且控制項工作正常。文件上傳,一切正常。

當我嘗試在我的實際應用程序中使用該控制項時,我總是收到一條錯誤消息,指出“附加的文件無效”。當上傳的文件為空時,AsyncFileUpload 控制項返回此值。

為了隔離問題,我創建了一個與展示應用程序中的母版頁完全相同的新母版頁。我還創建了一個與我的展示應用程序中的頁面完全相同的 aspx 頁面。上傳仍然失敗,並出現“附加的文件無效”錯誤。

我還比較了真實應用程序和展示應用程序的 web.config,無法確定任何重要的差異。

下面的程式碼是我真實應用中的測試程式碼。此程式碼與展示應用程序中的功能程式碼完全相同,只是具有不同的類名和文件名。下面列出的 web.config 來自刪除了 appsettings 和 connectionstrings 的真實應用程序。

在這一點上,我完全被難住了。

真實應用測試母版頁:

<%@ Master Language="C#" AutoEventWireup="true" CodeFile="aaaMasterPage.master.cs" Inherits="L1Admin.aaaMasterPage" %>

<!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>Untitled Page</title>
   <script type="text/javascript" src="js/jquery-1.3.2.min.js"></script>    
   <asp:ContentPlaceHolder id="head" runat="server">
   </asp:ContentPlaceHolder>
</head>
<body>
   <form id="form1" runat="server">
   <asp:ScriptManager ID="scriptManager" EnablePartialRendering="true" runat="server" AsyncPostBackTimeout="180" />    
   <div>
       <asp:ContentPlaceHolder id="content" runat="server">

       </asp:ContentPlaceHolder>
   </div>
   </form>
</body>
</html>

測試頁面:

<%@ Page Language="C#" MasterPageFile="~/aaaMasterPage.master" AutoEventWireup="true" CodeFile="aaaFileUploadTest2.aspx.cs" Inherits="aaaFileUploadTest2" Title="Untitled Page" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajax" %>

   <asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
       <script type="text/javascript">
           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();
           }
       </script>
   </asp:Content>
   <asp:Content ID="Content2" ContentPlaceHolderID="content" Runat="Server">    
       <div id="uploadMessage"><p></p></div>
       <ajax:AsyncFileUpload ID="pageBannerUpload"
           CssClass="file_upload"
           OnClientUploadError="uploadError"
           OnClientUploadStarted="startUpload"
           OnClientUploadComplete="uploadComplete"
           onuploadedcomplete="upload_UploadedComplete"
           runat="server" />
   </asp:Content>

真實應用測試頁面程式碼背後:

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);
       }
   }
}

真實應用測試 Web.config:

<?xml version="1.0"?>
<!-- 
   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->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 
-->
<configuration>
   <configSections>
       <sectionGroup name="system.web.extensions" type="System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
           <sectionGroup name="scripting" type="System.Web.Configuration.ScriptingSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
               <section name="scriptResourceHandler" type="System.Web.Configuration.ScriptingScriptResourceHandlerSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
               <sectionGroup name="webServices" type="System.Web.Configuration.ScriptingWebServicesSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
                   <section name="jsonSerialization" type="System.Web.Configuration.ScriptingJsonSerializationSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="Everywhere"/>
                   <section name="profileService" type="System.Web.Configuration.ScriptingProfileServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
                   <section name="authenticationService" type="System.Web.Configuration.ScriptingAuthenticationServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
                   <section name="roleService" type="System.Web.Configuration.ScriptingRoleServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
               </sectionGroup>
           </sectionGroup>
       </sectionGroup>
   </configSections>
   <appSettings>

   </appSettings>
 <connectionStrings>
 </connectionStrings>
   <system.web>
       <!-- 
           Set compilation debug="true" to insert debugging 
           symbols into the compiled page. Because this 
           affects performance, set this value to true only 
           during development.
       -->
       <compilation debug="true">
           <assemblies>
               <add assembly="System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
               <add assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
               <add assembly="System.Xml.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
               <add assembly="System.Data.DataSetExtensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
               <add assembly="System.Transactions, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
               <add assembly="System.Data.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
           </assemblies>
           <codeSubDirectories>
               <add directoryName="CSCode"/>
               <add directoryName="VBCode"/>
           </codeSubDirectories>
       </compilation>
       <!--
           The <authentication> section enables configuration 
           of the security authentication mode used by 
           ASP.NET to identify an incoming user. 
       -->
       <authentication mode="Windows"/>
       <!--
           The <customErrors> 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.

       <customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
           <error statusCode="403" redirect="NoAccess.htm" />
           <error statusCode="404" redirect="FileNotFound.htm" />
       </customErrors>
       -->
       <pages>
           <controls>
               <add tagPrefix="asp" namespace="System.Web.UI" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
               <add tagPrefix="asp" namespace="System.Web.UI.WebControls" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
               <add tagPrefix="Annsa" namespace="Annsa.Controls" assembly="Annsa.Controls"/>
           </controls>
           <namespaces>
               <add namespace="Microsoft.VisualBasic"/>
               <add namespace="System.Data"/>
               <add namespace="System.Drawing"/>
           </namespaces>
       </pages>
       <httpHandlers>
           &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;
       <httpModules>
           &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;

編輯:我不再為這家公司工作,所以我無法測試任何新答案以查看它們是否解決了問題。

我在 .NET 4 中遇到了同樣的問題。我通過在控制項上設置 ClientIDMode=“AutoID” 來解決它。

如果您已遷移 webproject,則 user423430 的解決方案是正確的:當您將 VS 2003 Web Project 自動遷移到 VS 2005 時,Web.config 會自動修改。&lt;xhtmlConformance mode="Legacy" /&gt;被添加在其中。建議不要將它與 ajax 一起使用:

http://weblogs.asp.net/scottgu/archive/2006/12/10/gotcha-don-t-use-xhtmlconformance-mode-legacy-with-asp-net-ajax.aspx

(ASP.NET 1.0 和 1.1 沒有從它的許多伺服器控制項發出 XHTML 兼容標記。ASP.NET 2.0 改變了這一點,預設情況下從所有控制項發出 XHTML 兼容標記。)

引用自:https://stackoverflow.com/questions/2314450