- Redirect after upload -

JFileUpload allows to redirect to any web page after successfull upload. It could be useful to notify after upload, display upload status, log filenames uploaded, store information in database and more.

Forward and post parameters

JFileUpload provides two parameters "post" and "forward" with similar behavior:

  • "forward" is a real redirect to a web page. It drives the underlying browser to display the URL setup in this parameter:
    <PARAM NAME="forward" VALUE="http://server.com/page.php">
    The URL must be absolute and not relative. If you need to pass your own parameters then just append them as any URL parameter:
    <PARAM NAME="forward" VALUE="http://server.com/page.php?p1=abc&p2=123">
    The page.php will receive an HTTP GET request and the generated HTML will be displayed in the browser.
  • "post" won't redirect to the specified URL. It only sends an HTTP POST request and discards the HTML response:
    <PARAM NAME="post" VALUE="http://server.com/path/script.php?p1=abc&p2=123">
    The URL must be absolute and not relative. "post" is mainly used to send status silently about upload on server-side without any end-user interaction. "forward" is really to redirect web browser to another page.

Note that both "post" and "forward" could be used together. "post" will execute first followed by "forward". Two others parameters work with "post" and "forward": "postparameters" and "forwardparameters". They have exactly the same behavior. They allow to pass details about upload such as filename, filesize, folder structure and timestamp. Values could be:

  • true: It passes each filename uploaded as: filename1=file1.ext&filename2=file2.ext...

  • short: It passes each filename uploaded as: filename=file1.ext;file2.ext

  • long: It passes each filename with absolute path selected by end-user as:

  • longer: It passes each filename with absolute path and filesize as:

  • extra: It passes each filename with absolute path, filesize, last modified timestamp and extra parameters setup in applet through paramX/valueX. It could be useful when extra parameter are setup with JavaScript with end-user form interaction:
    filedate is milliseconds from epoch (January 1st, 1970). For instance, 1191938466593 means October 09th, 2007, 3:01:06 PM GMT+1

  • extrarelative: It passes each filename with relative path and extra parameters. It could be useful when you need to know the relative folder structure that has been uploaded:
    To make it works you need to setup folderdepth, relativefilename and notifyrelativefilename parameters as following:
    <PARAM NAME="folderdepth" VALUE="-1">
    <PARAM NAME="param3" VALUE="relativefilename">
    <PARAM NAME="value3" VALUE="true">
    <PARAM NAME="param4" VALUE="notifyrelativefilename">
    <PARAM NAME="value4" VALUE="true">
    <PARAM NAME="post" VALUE="http://server.com/script.php">
    <PARAM NAME="postparameters" VALUE="extrarelative">

Each upload detail is then available on server-side as it was sent by a simple HTML form. It means that they can be read with standard API for HTTP GET or POST.

What's wrong with forward parameter ?
Uploading hundred files with "forward" and "forwardparameters" enabled will generate very long URLs when redirecting. It could be a problem with some browsers that limit URL size. Some will cut some parameters and others won't redirect. That's the reason why "post", "postparameters" and "forward" without "forwardparameters" is recommended. "post" and "postparameters" is safe to send upload details in HTTP POST request (which is not limited by size) and then "forward" to redirect. You can pass an additional session identifier if you need to share data between "post" and "forward" scripts:
<PARAM NAME="post" VALUE="http://server.com/script.php?sessionid=ABC123">
<PARAM NAME="postparameters" VALUE="true">
<PARAM NAME="forward" VALUE="http://server.com/page.php?sessionid=ABC123">

Send email after upload
Here is a sample of "post" and "forward" usage to send an email after upload and redirect to a generic HTML page:
<PARAM NAME="post" VALUE="http://server.com/email.php">
<PARAM NAME="postparameters" VALUE="true">
<PARAM NAME="forward" VALUE="http://server.com/redirect.html">
email.php is a PHP script allowing to send an email from server-side. It includes filenames uploaded in the email content.

Java and all Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the U.S. and other countries.
All other company and/or product names are the property of their respective owners.