Shantaram Tupe
Shantaram Tupe

Reputation: 1666

Upload multiple files with maintaining Index ( position at which file was uploaded) in Struts2

I have scenario that, I want to upload multiple files, In which User may or may not upload files, And I want to maintain Index at which position user has uploaded file and want to save file with that index as Name

I referred https://stackoverflow.com/a/17050230/3425489 , In my case I don't want to create new class, so not referred Accepted solution

till now In my Action Class I have

File upload [];
String uploadContentType []
String uploadFileName []

getters and setters

In my jsp I tried

<input type="file" name="upload">

but I'm able to get uploaded files only, not able to maintain index

also tried

<input type="file" name="upload[0]">
<input type="file" name="upload[1]">
<input type="file" name="upload[2]">

with this case, I'm not able to setProperties in my Action class

----Updated----

You can refer my Model Struts 2 : Unable to access Model properties in JSP

For your every ProcessSolutionStep, I want to maintain, which file is uploaded for particular step,

i.e. User may upload file for step 1 and step 5, skipping middle steps, and in view.

I want to display file uploaded for particular step

Upvotes: 1

Views: 1813

Answers (2)

Shantaram Tupe
Shantaram Tupe

Reputation: 1666

This is How I solved my Problem:

    <tr>
    <td> Step 1 :
    <td>
        <input type="hidden" name="isFileUpload" id="id-is-file-upload-0" value="0">
        <textarea id="id-solution-0" name="processSolutionSteps" rows="2" cols="50" maxlength="200" class="class-text-area class-text-area-not-blank"></textarea>
        <!-- <input type="text" id="id-solution-0" name="processSolution" maxlength="30" size="35"> -->
        <p id="id-process-solution-counter-0"></p>
    </td>
    <td>
        <input type="file" id="id-file-0" name="uploads">
    </td>
</tr>
<tr>
    <td> Step 2 :
    <td>
        <input type="hidden" name="isFileUpload" id="id-is-file-upload-1" value="0">
        <textarea id="id-solution-1" name="processSolutionSteps" rows="2" cols="50" maxlength="200" class="class-text-area class-text-area-not-blank"></textarea>
        <!-- <input type="text" id="id-solution-1" name="processSolution" size="35"> -->
        <p id="id-process-solution-counter-1"></p>
    </td>
    <td>
        <input type="file" id="id-file-1" name="uploads">
    </td>
</tr>
<tr>
    <td> Step 3 :
    <td>
        <input type="hidden" name="isFileUpload" id="id-is-file-upload-2" value="0">
        <textarea id="id-solution-2" name="processSolutionSteps" rows="2" cols="50" maxlength="200" class="class-text-area class-text-area-not-blank"></textarea>
        <!-- <input type="text" id="id-solution-2" name="processSolution" size="35"> -->
        <p id="id-process-solution-counter-2"></p>
    </td>
    <td>
        <input type="file" id="id-file-2" name="uploads">
    </td>
</tr>

I just post few sample code of my <tr> tag

I have maintained one hidden field isFileUpload with inital value 0 , as many no. of my <input type="file">, After uploading file, on its change event I changed value of isFileUpload to 1 as

$('#id-solution-table').on('change', 'input[type=file]', function () {
    $('#id-is-file-upload-'+$(this).prop("id").split("-")[2]).val(1);
});

And In my Action Class I have this code

Depending upon values of isFileUpload i.e. I have checked it with 1,

Means I have uploded file at this index position and Mapped with uploaded file array which is uploads

private File [] uploads;
private String [] uploadsFileName;
private String [] uploadsContentType;
private short isFileUpload [];

try {
    int fileIndex = 0;
    for (int i = 0; i < this.isFileUpload.length; i++) {
        if( this.isFileUpload[i] == 1 ) {
            System.out.println(" index    "+i+ " isFileUpload "+this.isFileUpload[i]);
            System.out.println("Index       "+i+ "     "+this.uploadsFileName[ fileIndex ]);
            String filePath = path;
            new File(filePath).mkdirs();
            FileUtils.copyFile(this.uploads[ fileIndex ], new File(filePath+"/"+i+"."+FilenameUtils.getExtension(this.uploadsFileName[ fileIndex ])));
            ++fileIndex;
        }
    }
} catch(Exception exception) {
    addActionError("Some files not uploaded.");
    exception.printStackTrace();
}

Upvotes: 1

Andrea Ligios
Andrea Ligios

Reputation: 50203

No need to create a new class (that is one way, if you prefer to encapsulate every object singularly), just use Lists:

public class Upload extends ActionSupport{

    private List<File> files;
    private List<String> filesContentType;
    private List<String> filesFileName;

    /* GETTERS AND SETTERS */           

    public String execute() throws Exception{
        System.out.print("\n\n---------------------------------------");
        int i=0;
        for (File file : files){
            System.out.print("\nFile ["+i+"] ");
            System.out.print("; name:"         + filesFileName.get(i));
            System.out.print("; contentType: " + filesContentType.get(i));
            System.out.print("; length: "      + file.length());
            i++;
        }
        System.out.println("\n---------------------------------------\n");
        return SUCCESS;
    }

}

Use the multiple attribute and don't forget the right enctype:

<s:form action="upload" enctype="multipart/form-data" >
    <s:file name="files" multiple="multiple" />
    <s:submit value="Upload files" />
</s:form>

Upvotes: 1

Related Questions