And it's called with
- function errorHandler( error ) { // Routine to call if an error is found
- alert( error ); // Some code to handle the error
- }
- downloadFileXfer( "serverroutinename.php", "someform", errorHandler ); // Download the file
On the first call this routine creates an iframe and attaches a load event handler to it, then submits the users form to the server.
If there is an error at the server end then this load handler picks up the return and passes it to your error handling routine.
If there is no error the "header" routine in the PHP code tells the browser to handle a file download and the load handler never triggers.
Subsequent calls to download other data detect the iframe exists so new ones aren't added and the load event routine replaces the previous one so a different error handler can be specified, if you've only got one error handler then you don't need to reattach the load event handler and this can be moved in with the iframe creation code.
As long as you gather the data and do any error checking before dumping the data this works nicely.
You can change the mime type at the server end for different types of data.
Saves a bit of housework on the server, I've no doubt there are errors and probably improvements people can make, so feel free, hope it's useful to someone.