Link to home
Start Free TrialLog in
Avatar of David Bach
David BachFlag for United States of America

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:

System.IO.DirectoryNotFoundException
  HResult=0x80070003
  Message=Could not find a part of the path 'C:\WINDOWS\TEMP\_AjaxFileUpload\974AB760-DCFE-2E04-E0D9-A0EE7E73221A\IMG_3001.JPG.tmp'.
  Source=mscorlib
  StackTrace:
   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   at System.IO.File.InternalCopy(String sourceFileName, String destFileName, Boolean overwrite, Boolean checkHost)
   at AjaxControlToolkit.AjaxFileUpload.SaveAs(String fileName)
   at Graphic.AjaxFileUpload1_UploadComplete(Object sender, AjaxFileUploadEventArgs e) in C:\inetpub\ClientWebs\AJAX File Upload Test\Graphic.aspx.vb:line 13
   at System.EventHandler`1.Invoke(Object sender, TEventArgs e)
   at AjaxControlToolkit.AjaxFileUpload.UploadRequestProcessor.XhrDone(String fileId)
   at AjaxControlToolkit.AjaxFileUpload.UploadRequestProcessor.ProcessRequest()

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>

Open in new window

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

Open in new window

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>

Open in new window

Any assistance you might give as to why the routine is called twice and not just once is most appreciated.

Thank you,
David Bach
Avatar of Robb Hill
Robb Hill
Flag of United States of America image

have you done the basic debugging to try and catch the other call.   Try using breakpoints and follow your code...also you f12 dev tools in chrome or other browser and follow the UI trail.

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.
Avatar of 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
Hmmm.  Something else in that code path has to be triggering a post.  Can you out breaks earlier in your code.
Hi Robb:

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.
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
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.IsInFileUploadPostBack 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.Contains("POST") && Request.Url.AbsoluteUri.Contains("AjaxFileUpload1"))
          loadcontrol();
Google this if you want to find those links:)

AjaxFileUpload postback event from a usercontrol ASP.NET
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
ASKER CERTIFIED SOLUTION
Avatar of David Bach
David Bach
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial