## Aurigma Image Uploader Flash 1.0

# Debugging Server Scripts

- Collapse All Sections
- See Also
- Language Filter ( all languages )

When you make an error in your server upload script (specified with the upload.actionUrl property), Image Uploader Flash stops uploading, and a message appears saying that an internal server error (HTTP code 500) has occurred.

The present article describes how to handle and debug such errors depending on the server platform. Here we consider three approaches for each server platform:

- Error Logging approach is more convenient for production applications. It catches all the exceptions and writes error details in a log file; so, it does not show any information about errors to users.
- Handling Progress Event approach implies showing the server response when some error occurs. If displaying errors is enabled, this response contains error details. However, this approach requires some modifications of Image Uploader Flash embedding script.
- Using Sniffer allows you to view server response without necessity to modify embedding code, but it uses a third-party tool.

Additionally, ASP.NET environment provides built-in facilities for debugging web applications.

## Debugging in ASP.NET

### Error Logging

This strategy implies enclosing the part of the upload script which saves files in try/catch block and writing exception details in external log file. The code sample below demonstrates how to implement the LogError(String,String) method. This method creates a current date log file in the /Log folder of your application and writes error message and stack trace in it.

### C#

    private void LogError(string errorMessage, string stackTrace)
    {
        string path = "~/Logs/" + DateTime.Today.ToString("MM-dd-yy") + ".log";
        if (!File.Exists(System.Web.HttpContext.Current.Server.MapPath(path)))
        {
            File.Create(System.Web.HttpContext.Current.Server.MapPath(path)).Close();
        }

        using (StreamWriter w = File.AppendText(System.Web.HttpContext.Current.Server.MapPath(path)))
        {
            w.WriteLine("{0} Error: {1}",
    			DateTime.Now.ToString(System.Globalization.CultureInfo.InvariantCulture),
    			errorMessage);
            w.WriteLine(stackTrace);
            w.Flush();
            w.Close();
        }
    }

    private void Page_Load(System.Object sender, System.EventArgs e)
    {
        try
        {
            SaveFiles();
        }
        catch (Exception ex)
        {
           LogError(ex.Message, ex.StackTrace);
        }
    }

    private void SaveFiles()
    {
    	// ...
    	// Saving uploaded files
    	// ...
    }

This method writes error details in the following way:

    12/04/2009 10:14:55 Error: Object reference not set to an instance of an object.
    	at ASP.basicdemo_upload_aspx.SaveFiles() in c:\inetpub\wwwroot\FlashUploader\BasicDemo\upload.aspx:line 129
    	at ASP.basicdemo_upload_aspx.Page_Load(Object sender, EventArgs e) in c:\inetpub\wwwroot\FlashUploader\BasicDemo\upload.aspx:line 61

### Handling Progress Event

Image Uploader Flash exposes the Progress event which allows handling the upload process including server-side errors. When client or server-side error occurs, this event fires with the status parameter equal to ERROR. Here the value parameter contains the error code and the statusText value represents the error description. In the case of a server-side error this parameter contains response from the page where files are uploaded. Thus, you can use this response to obtain upload error details.

### ASP.NET

    <script type="text/javascript">
    function progress(status, progress, valueMax, value, statusText){
    	//Some server-side error occured
    	if (status == "ERROR" && value == 4){
    		var newWindow = window.open();
    		with (newWindow.document) {
    			open("text/html");
    			write(statusText);
    			close();
    		}
    	}
    }
    </script>

    <aur:ImageUploaderFlash ID="Uploader1" runat="server">
        <ClientEvents>
            <aur:ClientEvent EventName="Progress" HandlerName="progress" />
        </ClientEvents>
    </aur:ImageUploaderFlash>

If you are not familiar with the Progress event read the Using Image Uploader Flash at Runtime topic first.

### Using Sniffer

Sniffer is a network analyzer software which allows tracking interactions between client-side and server-side parts of an application. Here we consider the Fiddler tool as an example of debugging server-side upload script.

1. Open default.aspx in your browser. If it is able to send traffic to Fiddler, the corresponded session will be displayed in the Web Sessions pane of the tool.
2. Send a file via Image Uploader Flash.

3. Double-click the upload.aspx session in the Web Sessions pane.

   Find upload session details in the left part of Fiddler's interface. Here it contains the POST request sent by Image Uploader Flash and the response returned by your upload script. So, if your upload script contains an error this error details will be displayed in the returned response.

### Running Script in Debug Mode

Visual Studio allows debugging ASP.NET Web applications in the same manner as Windows Forms. See the Debugging ASP.NET Web Applications During Development and Debugging Deployed ASP.NET Web Applications MSDN topics for step-by-step instructions on how to debug ASP.NET Web applications.

## Debugging in PHP

### Error Logging

PHP configuration file (php.ini) contains the Error handling and logging section which controls how errors are handled and reported. To configure error logging set the following parameters:

    error_reporting  =  E_ALL
    log_errors = On

The first one defines what error levels should be handled. We set E_ALL which means: all errors, warnings, and notices. The second parameter states whether these error reports should be written to a log file. If On, as we have set, error messages will be written to the <Apache installation folder>\logs\error.log file in the following way:

[Sat Dec 04 12:27:53 2009] [error] [client 127.0.0.1] PHP Warning: copy(Gallery/Thumbnails/Forest%20Flowers.jpg): failed to open stream: No such file or directory in P:\\home\\localhost\\www\\PHPLibrary\\FlashUploaderPHP\\flashrequestfix.php on line 53, referer: http://localhost/phplibrary/flashuploaderphp/scripts/aurigma.uploader.swf

Optionally, you can set a limitation for the error file length using the log_errors_max_len parameter.

### Handling Progress Event

The difficulty with debugging PHP applications is that they may not return HTTP code 500 when some error occurs. So, the Progress event will not indicate a server-side error (status is ERROR) when it occurs. To bypass this limitation we can handle this event on the upload completion step (status is COMPLETE) and display the statusText value when it does not equal some predefined response.

For example, your server-side upload script returns a "complete" string (predefined response) after all the uploaded files were saved.

### PHP

    <?php
    // ... save uploaded files

    echo "complete";
    ?>

The following code shows how to handle the Progress event in this case to display server-side error details.

### PHP

    <script type="text/javascript">
    function progress(status, progress, valueMax, value, statusText){
        //Some server-side error occured
        if (status == "COMPLETE" && statusText != "complete"){
            var newWindow = window.open();
            with (newWindow.document) {
                open("text/html");
                write(statusText);
                close();
            }
        }
    }
    </script>

    <?php
    $uploader = new ImageUploaderFlash();
    $uploader->setID("Uploader1");

    $uploader->getClientEvents()->setProgress(array("progress"));

    $uploader->render();
    ?>

Note

If error details are not displayed, make sure that display_errors parameter in your php.ini is set to On.

If you are not familiar with the Progress event read the Using Image Uploader Flash at Runtime topic first.

### Using Sniffer

As an example of network analyzer tool we use the Fiddler. Here we discuss how to use it to debug server-side upload script.

1. Open index.php in your browser. If it is able to send traffic to Fiddler, the corresponded session will be displayed in the Web Sessions pane of the tool.
2. Send a file via Image Uploader Flash.

3. Double-click the upload.php session in the Web Sessions pane.

   Find upload session details in the left part of Fiddler's interface. Here it contains the POST request sent by Image Uploader Flash and the response returned by your upload script. So, if your upload script contains an error this error details will be displayed in the returned response.

   Note

   If error details are not displayed, make sure that display_errors parameter in your php.ini is set to On.

## See Also

### Reference

- upload.actionUrl Property

### Manual

- Saving Uploaded Files in ASP.NET
- Saving Uploaded Files in PHP

### Other Resources

- Debugging ASP.NET Web Applications During Development
- Debugging Deployed ASP.NET Web Applications

- ActionUrl (ASP.NET)
- ActionUrl (PHP)
- actionUrl (Java Script)

- ActionUrl (ASP.NET)
- ActionUrl (PHP)
- actionUrl (Java Script)