You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cocoon.apache.org by lo...@gmx.at on 2006/04/24 15:42:04 UTC

Problem with Fileupload

Hi!

I tried the following code to upload a file, unfortunately the upload
doesn't work since the fileItems.size() == 0.
So I guess something is wrong with the HttpRequest.
I post the xsp, the HTML Form and the pattern from the sitemap.
Maybe someone has an idea what I'm doing wrong.
regards, Markus

the XSP:
<xsp:logic>
      // get the httpservletrequest from Cocoon

    HttpServletRequest objRequest =
(HttpServletRequest)objectModel.get(HttpEnvironment.HTTP_REQUEST_OBJECT);

      // define some constants
      long lMaxSize = 10000000;   // (10 Mb)
      int iSizeThreshold = 1024;  // bytes to hold in memory before writing
to temp location
      String strPath = "C:/tmp"; // temp location

      // create the uploader
      DiskFileUpload objFileUploads = new DiskFileUpload();

      // maximum size that will be stored in memory
      objFileUploads.setSizeThreshold(iSizeThreshold);

      // the temp location for saving data that is larger than
      //getSizeThreshold()
      objFileUploads.setRepositoryPath(strPath);

      try {
        List fileItems = objFileUploads.parseRequest(objRequest,
iSizeThreshold, lMaxSize, strPath);
        System.out.println("Number of Files : " + fileItems.size());

        Iterator it = fileItems.iterator();

        for (int i = 0; i != fileItems.size(); i++) {
          FileItem fi = (FileItem) it.next();

          // write the file to the disk
          System.out.println("File " + fi.getName() + " field " +
fi.getFieldName() + " size " + fi.getSize());
          File file = new File(strPath + "/" + i);
          file.createNewFile();
          // write the incomming file (fi) to the disk file created above
          fi.write(file);

        }
      } catch (FileUploadException fue) {
         fue.printStackTrace();
      } catch (Exception e) {
         e.printStackTrace();
      }

  </xsp:logic>

my HTML-Form:

form action="uploadtest" method="post" enctype="multipart/form-data">
  <input type="file" name="myFile">
  <input type="submit" value="Upload">
  </form>

the entry in the sitemap:
<map:match pattern="uploadtest">
          <map:generate type="xsp" src="xsp/upload.xsp" />
          <map:serialize type="xml" />
      </map:match>

-- 
"Feel free" - 10 GB Mailbox, 100 FreeSMS/Monat ...
Jetzt GMX TopMail testen: http://www.gmx.net/de/go/topmail

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org


Re: Problem with Fileupload

Posted by "John L. Webber" <jo...@jentro.com>.
Hi Markus,

I didn't examine your code at depth, but could the problem be referring
to your temp file as "C:/tmp" instead of "C:\tmp"?

John

looptag@gmx.at wrote:
> Hi!
> 
> I tried the following code to upload a file, unfortunately the upload
> doesn't work since the fileItems.size() == 0.
> So I guess something is wrong with the HttpRequest.
> I post the xsp, the HTML Form and the pattern from the sitemap.
> Maybe someone has an idea what I'm doing wrong.
> regards, Markus
> 
> the XSP:
> <xsp:logic>
>       // get the httpservletrequest from Cocoon
> 
>     HttpServletRequest objRequest =
> (HttpServletRequest)objectModel.get(HttpEnvironment.HTTP_REQUEST_OBJECT);
> 
>       // define some constants
>       long lMaxSize = 10000000;   // (10 Mb)
>       int iSizeThreshold = 1024;  // bytes to hold in memory before writing
> to temp location
>       String strPath = "C:/tmp"; // temp location
> 
>       // create the uploader
-- 
---------------------------------------------------------
 Jentro Technologies GmbH
 John L. Webber, Software Development
---------------------------------------------------------
 Rosenheimer Str. 145e	    81671 München
 Tel. +49 89 189 169 80     mailto:John.Webber@jentro.com
 Fax  +49 89 189 169 99     http://www.jentro.com
---------------------------------------------------------
NOTICE: The information contained in this e-mail is confidential or may
otherwise be legally privileged. It is intended for the named recipient
only. If you have received it in error, please notify us immediately by
reply or by calling the telephone number above and delete this message
and all its attachments without any use or further distribution of its
contents. Please note that any unauthorised review, copying, disclosing
or otherwise making use of the information is strictly prohibited. Thank
you.
---------------------------------------------------------

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org