David Bach
asked on
AJAX Routine AjaxFileUpload Runing Twice
Greetings:
I'm using AJAX AjaxFileUpload.
My symptoms are the OnUploadComplete event is running twice when only 1 file is selected. The second time I receive an error as follows:
The AjaxFileUpload event results in this error because this event is entered a 2nd time for reasons I do not, as yet, understand.
My aspx page is as follows:
Thank you,
David Bach
I'm using AJAX AjaxFileUpload.
My symptoms are the OnUploadComplete event is running twice when only 1 file is selected. The second time I receive an error as follows:
System.IO.DirectoryNotFoun dException
HResult=0x80070003
Message=Could not find a part of the path 'C:\WINDOWS\TEMP\_AjaxFile Upload\974 AB760-DCFE -2E04-E0D9 -A0EE7E732 21A\IMG_30 01.JPG.tmp '.
Source=mscorlib
StackTrace:
at System.IO.__Error.WinIOErr or(Int32 errorCode, String maybeFullPath)
at System.IO.File.InternalCop y(String sourceFileName, String destFileName, Boolean overwrite, Boolean checkHost)
at AjaxControlToolkit.AjaxFil eUpload.Sa veAs(Strin g fileName)
at Graphic.AjaxFileUpload1_Up loadComple te(Object sender, AjaxFileUploadEventArgs e) in C:\inetpub\ClientWebs\AJAX File Upload Test\Graphic.aspx.vb:line 13
at System.EventHandler`1.Invo ke(Object sender, TEventArgs e)
at AjaxControlToolkit.AjaxFil eUpload.Up loadReques tProcessor .XhrDone(S tring fileId)
at AjaxControlToolkit.AjaxFil eUpload.Up loadReques tProcessor .ProcessRe quest()
HResult=0x80070003
Message=Could not find a part of the path 'C:\WINDOWS\TEMP\_AjaxFile
Source=mscorlib
StackTrace:
at System.IO.__Error.WinIOErr
at System.IO.File.InternalCop
at AjaxControlToolkit.AjaxFil
at Graphic.AjaxFileUpload1_Up
at System.EventHandler`1.Invo
at AjaxControlToolkit.AjaxFil
at AjaxControlToolkit.AjaxFil
The AjaxFileUpload event results in this error because this event is entered a 2nd time for reasons I do not, as yet, understand.
My aspx page is as follows:
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Graphic.aspx.vb" Inherits="Graphic" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajaxToolkit" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>AJAX Synchronous File Upload Test</title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1"
runat="server"></asp:ScriptManager>
<ajaxToolkit:AjaxFileUpload ID="AjaxFileUpload1"
runat="server"
AllowedFileTypes="jpeg,jpg,gif"
OnUploadComplete="AjaxFileUpload1_UploadComplete"
ClientIDMode="AutoID"
AutoStartUpload="True"
ClearFileListAfterUpload="True"
MaximumNumberOfFiles="1" />
</form>
</body>
</html>
The code behind is:Imports AjaxControlToolkit
Imports System.IO
Partial Class Graphic
Inherits Page
Protected Sub AjaxFileUpload1_UploadComplete(sender As Object, e As AjaxFileUploadEventArgs) Handles AjaxFileUpload1.UploadComplete
Dim strServerPath As String = String.Empty
strServerPath = (Server.MapPath("~/Images/") & Convert.ToString(Guid.NewGuid()) & Path.GetFileName(e.FileName))
AjaxFileUpload1.SaveAs(strServerPath)
End Sub
End Class
And lastly the web.config:<?xml version="1.0"?>
<!--
https://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<system.web>
<compilation debug="true" strict="false" explicit="true" targetFramework="4.7.2"/>
<httpRuntime targetFramework="4.7.2" maxRequestLength="51200" />
<pages controlRenderingCompatibilityVersion="4.6" clientIDMode="AutoID">
<controls>
<add tagPrefix="ajaxToolkit" assembly="AjaxControlToolkit" namespace="AjaxControlToolkit"/>
</controls>
</pages>
</system.web>
<system.webServer>
<validation validateIntegratedModeConfiguration="false"/>
<handlers>
<add name="AjaxFileUploadHandler" verb="*" path="AjaxFileUploadHandler.axd" type="AjaxControlToolkit.AjaxFileUploadHandler, AjaxControlToolkit"/>
</handlers>
</system.webServer>
</configuration>
Any assistance you might give as to why the routine is called twice and not just once is most appreciated.Thank you,
David Bach
ASKER
Greetings Robb:
The routine invoked is an event handler for when the AJAX routine completes an upload of a graphic. Only 1 graphic is selected for upload and the routine is executed successfully. Subsequently the routine is called a 2nd time, however, there is no graphic data to act upon. The routine ends in error.
Yes, I see the routine is called twice via debugging and a break point, however, I do not see the conditions under which it's called.
Much thanks, Robb
The routine invoked is an event handler for when the AJAX routine completes an upload of a graphic. Only 1 graphic is selected for upload and the routine is executed successfully. Subsequently the routine is called a 2nd time, however, there is no graphic data to act upon. The routine ends in error.
Yes, I see the routine is called twice via debugging and a break point, however, I do not see the conditions under which it's called.
Much thanks, Robb
Hmmm. Something else in that code path has to be triggering a post. Can you out breaks earlier in your code.
ASKER
Hi Robb:
Forgive me for my ignorance, what is an "out break" please?
Much thanks, Robb
David Bach
Forgive me for my ignorance, what is an "out break" please?
Much thanks, Robb
David Bach
Sorry. My phone changed my typing.
I only meant to see if you can put some break points earlier in code to try and determine why it post twice.
Maybe you just need to handke the 2nd call so it doesn't process if that would be appropriate.
Atleast trap the error.
I only meant to see if you can put some break points earlier in code to try and determine why it post twice.
Maybe you just need to handke the 2nd call so it doesn't process if that would be appropriate.
Atleast trap the error.
ASKER
Hi Robb:
Your idea as a temporary fix is reasonable. I think there is still an issue with the AJAX routine or my code. Hopefully, it's me.
Much thanks, Robb,
David Bach
Your idea as a temporary fix is reasonable. I think there is still an issue with the AJAX routine or my code. Hopefully, it's me.
Much thanks, Robb,
David Bach
I think the control is probably created on the page so based on the asp.net page life cycle process the control is rendered more than once ...the 2nd time its not handled.
Perhaps controlling when its created is appropriate.
I found this example of it being created on page_init. There are probably a few other examples like this on the web but I bet its just how you implemented this control in the asp.net page. Just keep in mind how the control lifecycle works within the asp.net page lifecycle.
Got it working !!
Since i have to load the controls only then will i be able to access AjaxFileUpload.IsInFileUpl oadPostBac k property, i decided to take an unorthodox approach.
Added below code to Page_Init
//to load the controls if AsyncFileUpload causes a postback
//the URL should contain the ID of the control in its context query string
if (Request.HttpMethod.Contai ns("POST") && Request.Url.AbsoluteUri.Co ntains("Aj axFileUplo ad1"))
loadcontrol();
Perhaps controlling when its created is appropriate.
I found this example of it being created on page_init. There are probably a few other examples like this on the web but I bet its just how you implemented this control in the asp.net page. Just keep in mind how the control lifecycle works within the asp.net page lifecycle.
Got it working !!
Since i have to load the controls only then will i be able to access AjaxFileUpload.IsInFileUpl
Added below code to Page_Init
//to load the controls if AsyncFileUpload causes a postback
//the URL should contain the ID of the control in its context query string
if (Request.HttpMethod.Contai
loadcontrol();
Google this if you want to find those links:)
AjaxFileUpload postback event from a usercontrol ASP.NET
AjaxFileUpload postback event from a usercontrol ASP.NET
ASKER
Greetings Robb:
I appreciate your diligence, time and patience with me.
My sincere apologies for leaving this go for so long.
I'm not sure what I did, however, the symptoms went away and I am not able to reproduce them.
Your suggestions definitely helped me.
Much thanks,
David Bach
I appreciate your diligence, time and patience with me.
My sincere apologies for leaving this go for so long.
I'm not sure what I did, however, the symptoms went away and I am not able to reproduce them.
Your suggestions definitely helped me.
Much thanks,
David Bach
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Clearly if you say its called twice you will find it. You need to break on where all its called in code..and monitor that during debug to see why its happening.
Then refactor as necessary.