On different stages of the download process you may encounter errors that should be properly handled. Errors which occur when calling JavaScript functions generally can be detected by checking the return values, but this does not help much when you need to track problems that happen during data transmission. In this case, you can use special events provided by File Downloader for download status checking and error handling.
There are two special events that are raised when the download is finished. Using them you can detect why the process stopped: either because of the download completion or an error.
This event is raised when a download of a single file is finished. It has the following parameters:
Below is the example of handling this event.
<script language="javascript"> function onDownloadItemComplete(Result, ErrorPage, Url, FileName, ContentType, FileSize){ //The handler will display the error description switch (Result){ case 1: alert("An error has occurred while downloading the file " + FileName); break; case 4: alert("The server returned a wrong MIME type (should be " + ContentType + ") for the file " + FileName); break; case 5: alert("The server returned a wrong size (should be " + FileSize + ") for the file " + FileName); break; case 6: alert("An error has occurred while writing the file " + FileName + " to disk"); break; case 7: alert("The file " + FileName + " could not be found on the " + "server"); break; case 8: alert("Access to the file " + FileName + " is denied"); break; case 9: alert("Time out while downloading the file " + FileName); break; } } var fd = new FileDownloaderWriter("FileDownloader", 122, 44); fd.addEventListener("DownloadItemComplete", "onDownloadItemComplete"); fd.writeHtml(); </script>
All errors, that can be discovered using the DownloadItemComplete event, will be logged and displayed to the user after the download completion.
This event is raised when the whole download process is completed. It has only one parameter, Result, and some of the values of this parameter correspond to errors. They are illustrated in the following example.
<script language="javascript"> function onDownloadComplete(Result){ //The handler will display the error description switch (Result){ case 1: alert("An error has occurred while downloading the file list"); break; case 2: alert("An error has occurred while creating the file list"); break; case 3: alert("The file list is empty"); break; case 7: alert("The file list could not be found on the server"); break; case 8: alert("Access to the file list denied"); break; case 9: alert("Time out while downloading the file list"); break; } } var fd = new FileDownloaderWriter("FileDownloader", 122, 44); fd.addEventListener("DownloadComplete", "onDownloadComplete"); fd.writeHtml(); </script>
A more general way to track errors occurring during the operation of File Downloader is to use the Error event. This event is raised if something goes wrong when the control performs such actions as file list transmission or parsing, file transmission or saving, and so on. In this case and in case of other unexpected situations, handle the Error event.
This event provides five parameters:
10x
codes correspond to these errors.20x
codes correspond to these errors.30x
codes correspond to these errors.40x
codes correspond to these errors.500
.60x
codes correspond to these errors.To track the Error event, you need to register its handler. That is how this can be done.
<script language="javascript"> function onError(Code, Description, HttpCode, Page, Url, Index){ //The handler will display the error description returned by the server //If the server does not return any description, the internal error code //will be displayed. var newWindow = window.open(); with (newWindow.document){ open("text/html"); if (Page) write(Page); else write("<html><head>Error</head><body>The internal code of the " + "error is " + Code + ". The error description is " + Description + ".</body></html>"); close(); } } var fd = new FileDownloaderWriter("FileDownloader", 122, 44); fd.addEventListener("Error", "onError"); fd.writeHtml(); </script>