tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Shanx" <ad...@shanx.com>
Subject Re: Multipart form handling help
Date Thu, 23 Nov 2000 14:20:58 GMT
Hi,

Thanks to all that answered. I have figured out a simple but creative
approach which might be a BIT patchy, but it works like a charm for me. I
dont need to bother with MULTIPART stuff at all!

In essence, what I do is that when the form is submitted, I use simple
Javascript to change the path of the selected image to a URL such as :
     file://c:/myimage.gif

I pass this value to the jsp file, and put it in the IMG SRC of the next
page. Cool huh? Works just fine for my purposes, and is so much faster you
wont believe it - because there is no rigmarole of handling it with
MultipartRequest.Java stuff and no dealing with byte arrays and all that..!

I am including the code for the benefit of posterity. :)

Comments/caveats welcome,
</Shanx>


PS: There are two files: UPLOAD.HTM and the file to which the form is
submitted, UPLOAD.JSP..



--------------------------------------<UPLOAD.HTM>--------------------------
------------

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<html>
<head>
<title>File upload test</title>
<script>
function doConvertImagePath(imagePathField, relatedPathField)
{
    var theValue = document.theform.file.value;
    theValue = "file://" + theValue.replace('\\', '/');
     document.theform.file_path.value = theValue;
}

function doPreviewImage1()
{
    var theValue = document.theform.file.value;
    if (theValue != "")
    {
        theValue = "file://" + theValue.replace('\\', '/');
    }
    else
    {
        theValue = "blank.gif";
    }
    document.previewImage.src = theValue;
}

function compute()
{
    // Change all the related file paths
    doConvertImagePath("document.theform.file",
"document.theform.file_path");
    return true;
}

</script>
<style type="text/css">
<!--
td {  font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 8pt}
input {  font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 8pt}
-->
</style>
</head>

<body>

<form name="theform" action="upload.jsp" onSubmit="return compute();"
method="POST">
  <table width="100%" border="0" cellspacing="0" cellpadding="3">
    <tr>
      <td width="100"><b>Select file:</b></td>
      <td width="200">
        <input type="file" name="file" onChange="doPreviewImage1()"
onBlur="doPreviewImage1()">
        <br>
        <input type="hidden" name="file_path">
      </td>
      <td rowspan="2"><img name="previewImage" src="blank.gif" width="100"
height="100" style="display: none"></td>
    </tr>
    <tr>
      <td><b>Select desc:</b></td>
      <td>
        <input type="text" name="desc">
      </td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td> <br>
        <input type="submit" value="Preview.." name="submit">
      </td>
      <td>&nbsp;</td>
    </tr>
  </table>


  <br>
  <br>
</form>

</body>
</html>

--------------------------------------</UPLOAD.HTM>-------------------------
-------------




Now, in the JSP file, I just IMAGE SRC is to the passed value, as follows:


--------------------------------------<UPLOAD.JSP>--------------------------
------------
<%@ page language="java" session="true" %>

<font face=verdana size=1>
<b>The image</b>: <br>
<img src="<% out.print(request.getParameter("file_path")); %>">
<p>
<b>The description</b>: <br>
<% out.print(request.getParameter("desc")); %>
</font>

--------------------------------------</UPLOAD.JSP>-------------------------
-------------



Mime
View raw message