Simple file download for generated output

Simple file download for generated output

Hi all,

Posted this as an idea proposal, not sure if that's right, basically it's a solution to a problem I had so I'm giving the code away.

I found I needed to interrogate a database and save the output as a text file on the users PC.

Now I could have dumped the output into a temporary file and given the user a link, but I'm lazy and didn't want the housekeeping work of tidying server files up later.

If I just submitted a form and the server generated an error instead of a download then handling the error at the browser end could be messy.

So I came up with this.

In the server end PHP I have code similar to the following

  1. // Some verification code that gathers the required data and reports errors back to the target PC via Ajax and then exits with no further output
  2. // If any errors have occurred then this point will never be reached, it is vital that at this point no output has been generated
  3.  header( "Content-type: text/plain" );                                             // Set up a file download
  4.  header( "Content-Disposition: attachment; filename="somefile.someextension" );
  5. // Dump the data
  6. exit(0);
In the Jquery code I have
  1. function downloadFileXfer( urlTgt, formName, callBackFunc ) {         // Manage a file download and return

  2.  var iFrameName = "downloadFileXferIframe";                                 // Iframe name for download
  3.  if ( $( "#" + iFrameName ).length == 0 ) {                                      // If the iframe has not been defined


  4.   var iFrame = $( "<iframe name=\"" + iFrameName + "\" " +            // Create an iframe object
                  "id=\"" + iFrameName + "\" " +
                  "style=\"display: none\">" );


  5.   $( "body" ).append( iFrame );                                                      // Append the iframe to the body of the document
  6.  }
  7.  $( "#" + iFrameName ).on( "load", function() {                                // Create the routine to handle a load event


  8.   var retVal= $( "#" + iFrameName )[0].contentWindow.document.body.innerHTML;    // Retrieve the return value
  9.   if ( retVal.length != 0 ) {                                                               // If there is data to pick up


  10.    callBackFunc( retVal );                                                              // Pass the data back to the callback function
  11.   }
  12.  });
  13.  $( "#" + formName ).attr( "action", urlTgt )                                      // Set the target Url
  14.             .attr( "method", "post" )                                                     // Post method
  15.             .attr( "enctype", "multipart/form-data" )                                // Encoding type
  16.             .attr( "encoding", "multipart/form-data" )
  17.             .attr( "target", iFrameName )                                              // Target for returned data
  18.  $( "#" + formName ).submit();                                                       // Submit the form
  19. }
And it's called with
  1. function errorHandler( error ) {                                                         // Routine to call if an error is found
  2.  alert( error );                                                                                 // Some code to handle the error
  3. }
  4. 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.




























    • Topic Participants

    • karen