小编典典

文件上传| 没有页面刷新| struts2 | 没有闪光灯

ajax

问题描述:我想使用JSP创建文件上传屏幕。屏幕将允许用户在屏幕上选择多个文件,但所有文件只有一个“上传”按钮。单击上载按钮后,应在Action类中获取所有文件对象。

但是重要的是页面提交后不应刷新。在同一屏幕上还会显示其他信息,在文件上传过程中不应更改这些信息。

我的尝试:我使用了简单的struts2文件上传功能,效果很好。但是它正在刷新提交页面。我使用AJAX(JQuery)来解决此问题。我使用AJAX面临的问题是它没有将File对象设置为Action类的file属性。因此,我无法在Action类中获取文件对象并进一步处理。

谁能帮我这个忙。

我将附加到目前为止我尝试过的所有代码。

JSP

<s:form action="fileUpload" method="post" enctype="multipart/form-data" >
<s:file id="file" name="userImage" cssClass="fileUpload" cssStyle="fileUpload" />
<button id="px-submit">Upload</button>      
</s:form>

<script type="text/javascript">
jQuery(function($){
$('.fileUpload').fileUploader();
});
</script>

jQuery插件这是我使用过的jquery插件。

动作类

public class FileUploadAction extends ActionSupport{

private File userImage;

public File getUserImage() {
    return userImage;
}

public void setUserImage(File userImage) {
    this.userImage = userImage;
}

public String execute()
{
    try
    {
        System.out.println("file name: " + userImage.toString());

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

    return SUCCESS;
}

编辑:这是我的struts配置文件。

Struts.xml

<action name="commonDataImportAction_*" class="xxx.Action">
<result name="SUCCESS" type="stream">
            <param name="contentType">text/html</param>
             <param name="inputName">inputStream</param>
        </result>

由于文件对象未设置,我在这里得到了一个空指针。

请帮忙。

提前致谢。高拉夫


阅读 229

收藏
2020-07-26

共1个答案

小编典典

我正在使用相同的插件,对我来说效果很好。我在您的代码中看到的第一个问题是您尚未设置要提交的上传按钮的类型。

<button id="px-submit" type="submit">Upload</button>

希望这应该解决null指针除外。另外,如该插件的文档中所述,您需要返回一个json字符串

<div id='message'>success message</div>

成功上传。因此,您需要更改struts.xml映射。尝试此操作,如果您遇到任何其他问题,请立即与我联系。编辑:好的,这是您要求的我的代码

JSP

<form action="uploadImage" method="post" enctype="multipart/form-data">
   <input type="file" name="image" class="fileUpload" multiple/>
   <button id="px-submit" type="submit">Save all images</button>
   <button id="px-clear" type="reset">Clear all</button>
</form>

$('.fileUpload').fileUploader({
          autoUpload: false,
          buttonUpload: '#px-submit',
          buttonClear: '#px-clear',
});

动作课

您需要返回流结果。我使用的插件(struts2的jQuery插件),它采用的是变化很好地照顾,但你
必须使用它仅仅是因为这一要求,而不是我给你返回流结果的代码,而无需使用任何插件。(摘自从这里

public class UploadImageAction extends ActionSupport{
        private File image;
        private String imageContentType;
        private String imageFileName;
        //getter/setter for these
        public String execute() {
         String status="";         
        try{
              //save file code here    
         status="<div id='message'>successfully uploaded</div>"; //on success
         inputStream = new StringBufferInputStream(status);
        }catch(WhateverException e){
         status="<div id='status'>fail</div><div id='message'>Your fail message</div>"; //on error
         inputStream = new StringBufferInputStream(status);
         //other code
        }

        return SUCCESS;
    }
    private InputStream inputStream;

    public InputStream getInputStream() {
       return inputStream;
    }
}

struts.xml

<action name="fileUpload" class="com.xxx.action.UploadImageAction">  
    <result type="stream">
      <param name="contentType">text/html</param>
      <param name="inputName">inputStream</param>
    </result>
  </action>
2020-07-26