0

I want upload file excel using ajax in struts 2 but getting null, I use pure html tag in my jsp page, I think it`s not be a problem

this is what i try to :

jsp :

<form id="uploadImgForm" action="fileUploadAction" method="post" enctype="multipart/form-data">
    <input type="file" name="myFile" id="UploadFile">
    <input type="button" value="Upload" onclick="uplod()">
</form>

this is ajax function

ajax :

function uplod(){
    var form = $('#uploadImgForm')[0];
    var data = new FormData(form);
    $.ajax({
        type: "POST",
        enctype: 'multipart/form-data',
        url: "fileUploadAction",
        data : data,
        cache: false,
        processData: false,
        contentType: false,
        success: function() {
            console.log("SUCCESS ");
        },
        error: function() {
            console.log("ERROR");
        }
    });
}

struts 2 xml :

    <action name="fileUploadAction" class="id.co.yutaka.mandoc.action.tesUplod">
        <result type="json"></result>
    </action>

in class action :

public class tesUplod extends ActionSupport{

private File myFile;
String myFileFileName;

@Override
public String execute(){
    System.out.println("file ="+ myFile);
    System.out.println("file ="+ myFileFileName );
    return SUCCESS;
}

public File getMyFile() {
    return myFile;
}

public void setMyFile(File myFile) {
    this.myFile = myFile;
}

public String getMyFileFileName() {
    return myFileFileName;
}

public void setMyFileFileName(String myFileFileName) {
    this.myFileFileName = myFileFileName;
}
}

Output :

myFile =null
myFileFileName =null

1 Answers1

0

I solved this.

what i change is I use a normal action seting in xml

    <action name="fileUploadAction" class="id.co.yutaka.mandoc.action.tesUplod">
        <interceptor-ref name="loginStack"></interceptor-ref>
        <interceptor-ref name="fileUpload">
            <param name="maximumSize">100000000</param>
        </interceptor-ref>
        <result name="success"></result>
    </action> 

not use json xml action, I use this and I got a file and any other form value.

may this usefull for other