You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by lampa <la...@gmail.com> on 2009/06/14 18:00:05 UTC

multiple FileUpload Problem

Hi All:
  I can post the File to my action and store them in database using  tag:
<s:file name="upload" />.
  According to Struts 2 , the "name" must be assigned as "upload". However,
why not the "name" are assigned to different value? just like it :
  <s:file name="upload1" />
  <s:file name="upload2" />
  <s:file name="upload3"/>

  If so, we can very clear that  which input tag submit the upload file in
the JSP. Because after upload file is submitted, the upload file will be put
into a list, you cannot find which tag submit the upload file.

  I just think there is something inconvenient. 
  Who can give me some advices ?

Lampa
 
-- 
View this message in context: http://www.nabble.com/multiple-FileUpload-Problem-tp24023039p24023039.html
Sent from the Struts - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


[OT RANT] Re: multiple FileUpload Problem

Posted by Dave Newton <ne...@yahoo.com>.
Martin Gainty wrote:
> [200 lines of config, JSP, Java, and JavaDocs]

We should strive for clarity when helping people. When questions are 
prompted by lack of documentation specificity it's even *more* crucial.

Needlessly verbose responses obfuscate the essence of answers, and 
present a cognitive overhead disproportionate to those answers.

In this case, the issue was a simple misunderstanding on the OP's part. 
IMO pointing out the misunderstanding is sufficient: it's concise, 
direct, and entails essentially zero effort to explain, and, even more 
importantly, to comprehend.

Here we were confronted with a wall of configuration and code, each 
"chunk" of which must be parsed for potential meaning. I question the 
efficacy of this approach, and here's why:

> WEB-INF/classes/struts.xml contains the inclusion of struts-fileupload.xml as illustrated here
> <include file="struts-fileupload.xml" />
 > [elided]

Given that we have web-accessible source code, perhaps simple references 
would suffice. Including ancillary source seems gratuitous--particularly 
since none of the code addresses the original question directly.

> //$STRUTS2_HOME/apps/struts2-showcase-2.1.2/fileupload/upload-success.jsp
 > [elided]

This page shows the action properties of the form we've submitted a 
single file to. It isn't related to the OP's question at all. The 
original question had to do with uploading multiple files and why (in 
the OP's view) the files "had" to be named "upload".

So far none of the included code has addressed the original question, 
but we must still take the time to *know* that it doesn't address the 
original question.

> //$STRUTS2_HOME/apps/struts2-showcase-2.1.2/fileupload/multipleUploadUsingList.jsp
 > [elided]

This section of code is related to the question, but simply repeats the 
information available on the documentation page to which (I've assumed) 
the OP has referred.

It is *precisely* this information that mislead the OP in the first 
place. Re-presenting this information reinforces the original misconception.

> /* $Id: MultipleFileUploadUsingListAction.java 478625 2006-11-23 17:31:52Z wsmoak $
 > [elided]

This is the code for the multiple-file upload action. The comments I've 
made regarding the JSP apply equally here: this code fragment reinforces 
an unwarranted assumption.

> //if you want a more overt mechanism to identify which file you
 > will be processing you can use the status.index illustrated

While this is true, it does not address the core issue: we do *not* have 
to use a collection of files. We can simply create multiple File action 
properties (and their associated file information properties).

Please, please, *please* try to (a) answer the question asked, and (b) 
do so as clearly as possible.

Dave

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


RE: multiple FileUpload Problem

Posted by Martin Gainty <mg...@hotmail.com>.
the fileupload examples should be included in struts2-showcase webapp

WEB-INF/classes/struts.xml contains the inclusion of struts-fileupload.xml as illustrated here
<include file="struts-fileupload.xml" />

<!-- Where the struts-fileupload.xml contents contain -->
<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
    <package name="fileupload" extends="struts-default" namespace="/fileupload">
        
        <action name="upload" class="org.apache.struts2.showcase.fileupload.FileUploadAction" method="input">
            <result>upload.jsp</result>
        </action>

        <action name="doUpload" class="org.apache.struts2.showcase.fileupload.FileUploadAction" method="upload">
            <result name="input">upload.jsp</result>
            <result>upload-success.jsp</result>
        </action>
        
        <action name="multipleUploadUsingList">
            <result>multipleUploadUsingList.jsp</result>
        </action>
        
        <action name="doMultipleUploadUsingList" class="org.apache.struts2.showcase.fileupload.MultipleFileUploadUsingListAction" method="upload">
            <result>multipleUploadUsingList-success.jsp</result>
        </action>


        <action name="multipleUploadUsingArray">
            <result>multipleUploadUsingArray.jsp</result>
        </action>

        <action name="doMultipleUploadUsingArray" class="org.apache.struts2.showcase.fileupload.MultipleFileUploadUsingArrayAction" method="upload">
            <result>multipleUploadUsingArray-success.jsp</result>
        </action>


    </package>
</struts>

//$STRUTS2_HOME/apps/struts2-showcase-2.1.2/fileupload/upload-success.jsp
<%@ page 
    language="java" 
    contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
    <title>Showcase</title>
</head>
<body>
<h1>Fileupload sample</h1>
<p>
    <ul>
        <li>ContentType: <s:property value="uploadContentType" /></li>
        <li>FileName: <s:property value="uploadFileName" /></li>
        <li>File: <s:property value="upload" /></li>
        <li>Caption:<s:property value="caption" /></li>
    </ul>
</p>
</body>
</html>

//$STRUTS2_HOME/apps/struts2-showcase-2.1.2/fileupload/multipleUploadUsingList.jsp
<%@ page 
    language="java" 
    contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Showcase - fileupload - multiple fileupload using List</title>
</head>
<body>

<s:form action="doMultipleUploadUsingList" method="POST" enctype="multipart/form-data">
    <s:file label="File (1)" name="upload" />
    <s:file label="File (2)" name="upload" />
    <s:file label="File (3)" name="upload" />
    <s:submit />
</s:form>
</body>
</html>

<!-- notice the change in name to upload -->
//
/* $Id: MultipleFileUploadUsingListAction.java 478625 2006-11-23 17:31:52Z wsmoak $
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package org.apache.struts2.showcase.fileupload;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import com.opensymphony.xwork2.ActionSupport;
/**
 * Showcase action - multiple file upload using List
 * @version $Date: 2006-11-23 11:31:52 -0600 (Thu, 23 Nov 2006) $ $Id: MultipleFileUploadUsingListAction.java 478625 2006-11-23 17:31:52Z wsmoak $
 */
public class MultipleFileUploadUsingListAction extends ActionSupport {

    private List<File> uploads = new ArrayList<File>();
    private List<String> uploadFileNames = new ArrayList<String>();
    private List<String> uploadContentTypes = new ArrayList<String>();

    public List<File> getUpload() {
        return this.uploads;
    }
    public void setUpload(List<File> uploads) {
        this.uploads = uploads;
    }

    public List<String> getUploadFileName() {
        return this.uploadFileNames;
    }
    public void setUploadFileName(List<String> uploadFileNames) {
        this.uploadFileNames = uploadFileNames;
    }

    public List<String> getUploadContentType() {
        return this.uploadContentTypes;
    }
    public void setUploadContentType(List<String> contentTypes) {
        this.uploadContentTypes = contentTypes;
    }

    public String upload() throws Exception {

        System.out.println("\n\n upload1");
        System.out.println("files:");
        for (File u: uploads) {
//place the identifying mechanism for the filename here e.g. u.getName();
            System.out.println("*** "+u+"\t"+u.length());
        }
        System.out.println("filenames:");
        for (String n: uploadFileNames) {
//n must equal the u.getName from above
            System.out.println("*** "+n);
        }
        System.out.println("content types:");
        for (String c: uploadContentTypes) {
            System.out.println("*** "+c);
        }
        System.out.println("\n\n");
        return SUCCESS;
    }
}

//if you want a more overt mechanism to identify which file you will be processing you can use the status.index illustrated
<%@taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Insert title here</title>
</head>
<body>

<!-- uniqueness of the uploaded file is guaranteed by the Struts iterator tag #stat.index -->
<table border="1">
<s:iterator value="upload" status="stat">
<tr>
    <td>File <s:property value="%{#stat.index}" /></td>
    <td><s:property value="%{upload[#stat.index]}" /></td>
</tr>
</s:iterator>
</table>

<table border="1">
<s:iterator value="uploadFileName" status="stat">
<tr>
    <td>File Name <s:property value="%{#stat.index}" /></td>
    <td><s:property value="%{uploadFileName[#stat.index]}" /></td>
</tr>    
</s:iterator>
</table>

<table border="1">
<s:iterator value="uploadContentType" status="stat">
<tr>
    <td>Content Type <s:property value="%{#stat.index}" /></td>
    <td><s:property value="%{uploadContentType[#stat.index]}" /></td>
</tr>
</s:iterator>
</table>
</body>
</html>

HTH
Martin Gainty 
______________________________________________ 
Verzicht und Vertraulichkeitanmerkung/Note de déni et de confidentialité
 
Diese Nachricht ist vertraulich. Sollten Sie nicht der vorgesehene Empfaenger sein, so bitten wir hoeflich um eine Mitteilung. Jede unbefugte Weiterleitung oder Fertigung einer Kopie ist unzulaessig. Diese Nachricht dient lediglich dem Austausch von Informationen und entfaltet keine rechtliche Bindungswirkung. Aufgrund der leichten Manipulierbarkeit von E-Mails koennen wir keine Haftung fuer den Inhalt uebernehmen.
Ce message est confidentiel et peut être privilégié. Si vous n'êtes pas le destinataire prévu, nous te demandons avec bonté que pour satisfaire informez l'expéditeur. N'importe quelle diffusion non autorisée ou la copie de ceci est interdite. Ce message sert à l'information seulement et n'aura pas n'importe quel effet légalement obligatoire. Étant donné que les email peuvent facilement être sujets à la manipulation, nous ne pouvons accepter aucune responsabilité pour le contenu fourni.




> Date: Sun, 14 Jun 2009 09:00:05 -0700
> From: lampa2000@gmail.com
> To: user@struts.apache.org
> Subject: multiple FileUpload Problem
> 
> 
> Hi All:
>   I can post the File to my action and store them in database using  tag:
> <s:file name="upload" />.
>   According to Struts 2 , the "name" must be assigned as "upload". However,
> why not the "name" are assigned to different value? just like it :
>   <s:file name="upload1" />
>   <s:file name="upload2" />
>   <s:file name="upload3"/>
> 
>   If so, we can very clear that  which input tag submit the upload file in
> the JSP. Because after upload file is submitted, the upload file will be put
> into a list, you cannot find which tag submit the upload file.
> 
>   I just think there is something inconvenient. 
>   Who can give me some advices ?
> 
> Lampa
>  
> -- 
> View this message in context: http://www.nabble.com/multiple-FileUpload-Problem-tp24023039p24023039.html
> Sent from the Struts - User mailing list archive at Nabble.com.
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
> 

_________________________________________________________________
Hotmail® has ever-growing storage! Don’t worry about storage limits. 
http://windowslive.com/Tutorial/Hotmail/Storage?ocid=TXT_TAGLM_WL_HM_Tutorial_Storage_062009

Re: multiple FileUpload Problem

Posted by Dave Newton <ne...@yahoo.com>.
lampa wrote:
>   According to Struts 2 , the "name" must be assigned as "upload". However,
> why not the "name" are assigned to different value? just like it :
>   <s:file name="upload1" />
>   <s:file name="upload2" />
>   <s:file name="upload3"/>
> 
>   If so, we can very clear that  which input tag submit the upload file in
> the JSP. Because after upload file is submitted, the upload file will be put
> into a list, you cannot find which tag submit the upload file.
> 
>   I just think there is something inconvenient. 
>   Who can give me some advices ?

The example I assume you're referring to [1] is specifically using a 
File[] for the upload values. That shouldn't imply you *must* to use an 
array--only that you *may*.

Dave

[1] http://struts.apache.org/2.x/docs/file-upload.html

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org