2010-02-22 2 views
11

Sto provando a utilizzare il controllo AsyncFileUpload dall'Asp.net Ajax Control Toolkit (30/900 stabile stabile del 30 settembre 2009: 30930). Ho creato un'applicazione demo e il controllo funziona correttamente. Il caricamento dei file e tutto va bene.Asp.Net Ajax Toolkit AsyncFileUpload - "Il file è allegato non valido" errore

Quando si tenta di utilizzare il controllo nella mia applicazione reale, viene sempre visualizzato un messaggio di errore "Il file allegato non è valido". Il controllo AsyncFileUpload restituisce questo quando il file caricato è nullo.

Per isolare il problema, ho creato una nuova pagina master esattamente come la pagina principale nella mia app demo. Ho anche creato una pagina di aspx esattamente come la pagina nella mia app demo. Il caricamento non riesce ancora con un errore "Il file allegato non è valido".

Ho anche confrontato il web.config per l'app reale e l'app demo e non sono stato in grado di identificare eventuali differenze che dovrebbero essere importanti.

Il codice seguente è il codice di prova nella mia app reale. Questo codice è esattamente lo stesso del codice funzionante nell'app demo, con l'eccezione di avere nomi di classi e nomi di file diversi. Il web.config elencato di seguito proviene dall'app reale con le impostazioni e le connessioni rimosse.

A questo punto, sono completamente perplesso.

reale Test App Pagina master:

<%@ 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> 

pagina di prova:

<%@ 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> 

reale Test App Code Page Dietro:

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

reale Test App 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> 
      <remove verb="*" path="*.asmx"/> 
      <add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/> 
      <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"/> 
      <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"/> 
     </httpHandlers> 
     <httpModules> 
      <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/> 
     </httpModules> 
     <identity impersonate="true"/> 
     <trace enabled="false" requestLimit="10" pageOutput="false" traceMode="SortByTime" localOnly="true"/> 
     <!-- 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". 
     --> 
     <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"/> 
     <!-- GLOBALIZATION 
      This section sets the globalization settings of the application. 
     --> 
     <xhtmlConformance mode="Legacy"/> 
    </system.web> 
    <location path="Pages"> 
    <system.web> 
     <xhtmlConformance mode="Transitional"></xhtmlConformance> 
    </system.web> 
    </location> 
    <system.codedom> 
     <compilers> 
      <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CSharp.CSharpCodeProvider,System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" warningLevel="4"> 
       <providerOption name="CompilerVersion" value="v3.5"/> 
       <providerOption name="WarnAsError" value="false"/> 
      </compiler> 
      <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.VisualBasic.VBCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" warningLevel="4"> 
       <providerOption name="CompilerVersion" value="v3.5"/> 
       <providerOption name="OptionInfer" value="true"/> 
       <providerOption name="WarnAsError" value="false"/> 
      </compiler> 
     </compilers> 
    </system.codedom> 
    <system.webServer> 
     <validation validateIntegratedModeConfiguration="false"/> 
     <modules> 
      <remove name="ScriptModule"/> 
      <add name="ScriptModule" preCondition="managedHandler" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/> 
     </modules> 
     <handlers> 
      <remove name="WebServiceHandlerFactory-Integrated"/> 
      <remove name="ScriptHandlerFactory"/> 
      <remove name="ScriptHandlerFactoryAppServices"/> 
      <remove name="ScriptResource"/> 
      <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"/> 
      <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"/> 
      <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"/> 
     </handlers> 
    </system.webServer> 
    <runtime> 
     <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> 
      <dependentAssembly> 
       <assemblyIdentity name="System.Web.Extensions" publicKeyToken="31bf3856ad364e35"/> 
       <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="3.5.0.0"/> 
      </dependentAssembly> 
      <dependentAssembly> 
       <assemblyIdentity name="System.Web.Extensions.Design" publicKeyToken="31bf3856ad364e35"/> 
       <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="3.5.0.0"/> 
      </dependentAssembly> 
     </assemblyBinding> 
    </runtime> 
</configuration> 

Modifica: non sto più lavorando per questa azienda, quindi non posso testare nuove risposte per vedere se risolvono il problema.

+0

Ran il campione con un web.config di default, jQuery 1.4.1 e 3.5 AjaxControlToolkit .50731; cambiato la pagina master in un ToolkitScriptManager e ha funzionato bene. Ho ricevuto l'errore dopo essere passato al tuo web.config (creato anche cartelle App_Code \ CS | VBCode). – user423430

+0

nella funzione startUpload (sender, args) { $ ('# uploadMessage p'). Html(); $ ('# uploadMessage'). Hide(); } $ ('# uploadMessage p'). Html(); non fa nulla Restituirebbe l'html a una variabile o qualcosa del genere, ma se usato in questo modo non fa nulla, credo. Vuoi eliminare l'html esistente? $ ('# uploadMessage p'). html ('') o $ ('# uploadMessage p'). empty(); Dubito che questo sia il problema, ma comunque jQuery non è usato correttamente. – Hcabnettek

+0

questo è, la vera soluzione: http://forums.asp.net/p/1547959/3788044.aspx –

risposta

20

ho avuto lo stesso problema in .NET 4. Ho ottenuto che funziona con impostando il ClientIDMode = "Auto-ID" sul controllo.

+0

Il problema in questo specifico esempio .NET 3.5 è modifica dei delimitatori del nome di controllo da "_" a ":". Il controllo AsyncFileUpload esegue un lavoro di controllo piuttosto ossuto per assicurarsi che si supponga di elaborare il caricamento del file. http://ajaxcontroltoolkit.codeplex.com/workitem/26975 – user423430

+0

1 Questo ha risolto anche il mio problema. –

0

È possibile che si stia superando il limite di caricamento? Qual è la dimensione del file che stai caricando?

È possibile impostare il formato nel vostro web.config:

<system.web> 
    <httpRuntime executionTimeout="110" maxRequestLength="20000" /> 
</system.web> 
+0

22kb. Lo stesso file che funziona bene nell'applicazione demo. Modifica: non è specificato nemmeno nell'app demo, quindi presumo che utilizzino entrambi l'impostazione predefinita. –

+1

a volte l'ambiente demo è diverso dall'ambiente di produzione. Hai controllato i permessi sulla directory in cui il file viene salvato? – Pbirkoff

+0

Entrambe queste applicazioni erano in esecuzione sul mio computer di sviluppo. –

1

la soluzione di user423430 è quella giusta se è stato migrato il proprio webproject: quando si esegue automaticamente la migrazione di un VS 2003 Web Project su VS 2005, Web.config viene modificato automaticamente. <xhtmlConformance mode="Legacy" /> è aggiunto in esso. E 'raccomandato non utilizzarlo con l'Ajax a:

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 e 1.1 non emetteva markup XHTML da molti dei suoi controlli server ASP.NET 2.0 ha cambiato questo e per impostazione predefinita. emette markup XHTML compliant da tutti i controlli.)

0

Quando si utilizza "AsyncFileUpload" all'interno di un Gridiview, utilizzare ClientIDMode = "Auto-ID"