1. From the jsp page select the image (image path) for uploading. Let the
preview button be a submit button .

<form name ="frm" method="POST" action="$link.setAction('graphics')"
enctype="multipart/form-data">
<INPUT TYPE ="hidden" NAME ="filePath" size="39" value="C:\Program
Files\Tomcat 5.0\webapps\cevm\uploadedimages\"/>
<input type="submit" value="Preview"  style="width:80"
onClick="setHidValue()" >

function setHidValue()
    {
      document.frm.paramPassed.value="preview";
    }

 <input type="hidden" name="paramPassed" size="25"/>

ie when u click the preview button a java script function is called, in
which we assign a value to the hidden variable as preview. this value is
passed to the action file.

ur struts config file willl be

<form-bean  name="graphicsForm" type="GraphicsForm"/>

<action  path="/graphics"
           type="GraphicsInfoDetailsAction"
           name="graphicsForm"
           scope="request"
           input="/submit.vm">
           <forward name="preview" path="/preview.vm" />
 </action>

ur graphicsForm is

import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;
import org.apache.struts.upload.MultipartRequestHandler;



public class GraphicsForm extends ActionForm
{

    public static final String ERROR_PROPERTY_MAX_LENGTH_EXCEEDED =
"MaxLengthExceeded";


    /**
     * The value of the text the user has sent as form data
     */
    protected String theText;

    /**
     * The value of the embedded query string parameter
     */
    protected String queryParam;

    /**
     * Whether or not to write to a file
     */
    protected boolean writeFile;

    /**
     * The file that the user has uploaded
     */

     protected FormFile logoFile;





    /**
     * The file path to write to
     */
    protected String filePath;


    /**
     * Retrieve the value of the text the user has sent as form data
     */

    protected String paramPassed;



     public String getParamPassed() {
         return paramPassed;
     }

     public void setParamPassed(String argParamPassed) {
         this.paramPassed=argParamPassed;
     }


    /**
     * Retrieve the value of the query string parameter
     */
    public String getQueryParam() {
 // queryParam="Successful";
        return queryParam;
    }

    /**
     * Set the value of the query string parameter
     */
    public void setQueryParam(String queryParam) {
        this.queryParam = queryParam;
    }

    /**
     * Retrieve a representation of the logoFile the user has uploaded
     */
    public FormFile getLogoFile() {
        return logoFile;
    }


    /**
     * Set a representation of the logoFile the user has uploaded
     */
    public void setLogoFile(FormFile logoFile) {
        this.logoFile = logoFile;
    }

    /**
     * Set whether or not to write to a file
     */
    public void setWriteFile(boolean writeFile) {
        this.writeFile = writeFile;
    }

    /**
     * Get whether or not to write to a file
     */
    public boolean getWriteFile() {
        return writeFile;
    }

    /**
     * Set the path to write a file to
     */
    public void setFilePath(String filePath) {
        this.filePath = filePath;
    }

    /**
     * Get the path to write a file to
     */
    public String getFilePath() {
        return filePath;
    }

    public void reset() {
        writeFile = false;
    }

    /**
     * Check to make sure the client hasn't exceeded the maximum allowed
upload size inside of this
     * validate method.
     */
    public ActionErrors validate(ActionMapping mapping, HttpServletRequest
request)
    {
        ActionErrors errors = null;
        //has the maximum length been exceeded?
        Boolean maxLengthExceeded = (Boolean)

request.getAttribute(MultipartRequestHandler.ATTRIBUTE_MAX_LENGTH_EXCEEDED);
        if ((maxLengthExceeded != null) &&
(maxLengthExceeded.booleanValue()))
        {
            errors = new ActionErrors();
            errors.add(ERROR_PROPERTY_MAX_LENGTH_EXCEEDED, new
ActionError("maxLengthExceeded"));
        }


        return errors;

    }
}


ur action file will be

import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import java.util.Date;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.text.SimpleDateFormat;

import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;
import org.apache.struts.action.ActionServlet;


import java.sql.Connection;
import javax.sql.DataSource;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.SQLException;
import java.lang.String;
import java.util.ArrayList;


    public final class GraphicsInfoDetailsAction extends Action
     {

  public ActionForward execute(ActionMapping mapping,
                                 ActionForm form,
                                 HttpServletRequest request,
                                 HttpServletResponse response)
     {
               HttpSession session = request.getSession(false);




 if (form instanceof GraphicsForm) {

            //this line is here for when the input page is upload-utf8.jsp,
            //it sets the correct character encoding for the response
              String encoding = request.getCharacterEncoding();
               if ((encoding != null) &&
(encoding.equalsIgnoreCase("utf-8")))
                {
                 response.setContentType("text/html; charset=utf-8");
                }
          GraphicsForm theForm = (GraphicsForm) form;


                        String param1   = theForm.getParamPassed();


                      String target="success";
                      String submitName=null;
                      String param2="start";


                      FormFile file = theForm.getLogoFile();

                      //retrieve the file representation
                  if(param2.equals("preview")) {
                      target="preview";
                      //retrieve the file name
                      fileName= file.getFileName();

                      session.putValue("fileName",fileName);
                    //retrieve the content type
                      String contentType = file.getContentType();


                      String fileUploadPath = theForm.getFilePath();
                      String fileToWrite = fileUploadPath + fileName;

                     session.putValue("imagePath",fileToWrite );


                      boolean writeFile = true;

                     //retrieve the file size
                     String size = (file.getFileSize() + " bytes");

                     String data = null;
                     String fname = null;
                     String fpath = null;


                   try {
                    //retrieve the file data
                    ByteArrayOutputStream baos = new
ByteArrayOutputStream();
                    InputStream stream = file.getInputStream();
                    if (!writeFile) {
                      //only write files out that are less than 1MB
                    if (file.getFileSize() < (4*1024000)) {
                        byte[] buffer = new byte[8192];
                        int bytesRead = 0;
                        while ((bytesRead = stream.read(buffer, 0, 8192))
!= -1) {
                            baos.write(buffer, 0, bytesRead);
                        }
                        data = new String(baos.toByteArray());
                     }
                     else {
                        data = new String("The file is greater than 4MB, " +
                                " and has not been written to stream." +
                                " File Size: " + file.getFileSize() + "
bytes. This is a" +
                                " limitation of this particular web
application, hard-coded" +
                                " in
org.apache.struts.webapp.upload.UploadAction");
                    }
                }
                else {
                    //write the file to the file specified

                    OutputStream bos = new FileOutputStream(fileToWrite);
                    int bytesRead = 0;
                    byte[] buffer = new byte[8192];
                    while ((bytesRead = stream.read(buffer, 0, 8192)) != -1)
{
                        bos.write(buffer, 0, bytesRead);
                    }
                    bos.close();
                   data = "The file has been written to \"" +
theForm.getFilePath() + "\"";

                  fpath = fileUploadPath;

                }
                //close the stream
                stream.close();
            }
            catch (FileNotFoundException fnfe) {
                return null;
            }
            catch (IOException ioe) {
                return null;
             }

 }


                       //destroy the temporary file created
                   //     file.destroy();


                      return (mapping.findForward(target));
                    }

                     return null;

       }
        }


from the  action file i am putting the value of the filepath into a session
imagePath. this session is called in the
below jsp file for displaying the preview of the image. please note that we
are displaying the image of a uploaded file.this program is for uploading
images




2.Below will  be some contents of ur jsp page where u need to display your
preview image.

function getURLString()
    {

     var fn = document.frm.T1.value;
     var Img = new Image();
     Img.src = fn;
     document.frm.imageName.src = Img.src;

    }

The function is a java script which is called when the jsp page loads. ie
call getURLString() when this page loads and as shown below.
<BODY bgColor="#ffffff" onLoad="getURLString()">


There should be a hidden variable of T1 whose value is the path of the image
(eg c:\\image.jpg) in ur jsp page as shown below.
<input type=hidden name="T1" size="39" value=imagePath>

Last ur jsp file should contain a image tag for display of image.
<img border="0" name ="imageName" src="">

The value of the hidden name (imagePath) should be passed from the java
action file.




----- Original Message -----
From: "uma.k" <[EMAIL PROTECTED]>
To: "'Struts Users Mailing List'" <[EMAIL PROTECTED]>;
<[EMAIL PROTECTED]>
Sent: Saturday, December 18, 2004 11:54 AM
Subject: RE: Preview of an Image and text


> Hi Frank,
> Thanks for the reply. Your solution works good if I have to show preview
in
> the same page but I what if I need to show preview in the next JSP? How do
I
> carry the values/images?
>
> Uma
>
> -----Original Message-----
> From: Frank W. Zammetti [mailto:[EMAIL PROTECTED]
> Sent: Friday, December 17, 2004 10:44 PM
> To: Struts Users Mailing List
> Subject: Re: Preview of an Image and text
>
>
> You might consider doing it all on the client...
>
> <html>
> <head>
> <title>test</title>
> <script>
> function preview() {
> lyrPreview.innerHTML = "";
> lyrPreview.innerHTML += "Some message text...<br>";
> lyrPreview.innerHTML += "<img src=\"file://" + theForm.theFile.value +
> "\">";
> }
> </script>
> </head>
> <body>
> Select file, then click button to preview:<br>
> <form name="theForm">
> <input type="file" name="theFile">
> <input type="button" onClick="preview();" value="Preview message">
> </form>
> <br><br>
> <u>Preview:</u><br><br>
> <span id="lyrPreview"></span>
> </body>
> </html>
>
> Works on IE, can't say whether it does on anything else.  Of course,
> this will only work if you have enough information to construct the
> complete preview on the client at that point, but you could
> alternatively submit the form, then in the reply construct the file://
> reference to the image as I've done above.  That would probably give you
> the best of both worlds, and also remove any cross-browser concerns.
> But, if you can do it without touching the server, so much the better I
> figure.
>
> --
> Frank W. Zammetti
> Founder and Chief Software Architect
> Omnytex Technologies
> http://www.omnytex.com
>
> uma.k wrote:
> > Hi,
> > I have a form where the user is given the option to select a file to
> > upload(normally gif or jpg) before the user submits the file to the
> server,
> > I wanted to show him a preview of his message and image.
> >
> > How do I do that? Where do I store the image temporarly?
> >
> > Any solutions?
> >
> > Uma
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: [EMAIL PROTECTED]
> > For additional commands, e-mail: [EMAIL PROTECTED]
> >
> >
> >
> >
> >
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
>
>



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to