当前位置: 代码迷 >> Java Web开发 >> $.ajaxFileUpload 上传文件有关问题,在线求解
  详细解决方案

$.ajaxFileUpload 上传文件有关问题,在线求解

热度:572   发布时间:2016-04-17 10:50:57.0
$.ajaxFileUpload 上传文件问题,在线求解
小弟使用ajaxFileUpload插件上传文件,$.ajaxFileUpload方法中加入fileElementId:'fileUpload'属性后,无法访问到action,注释掉即可访问,很奇怪的问题,

我的struts2访问是没有问题的,可以正常访问,就是加入了fileElementId属性就不行了,很奇怪,不加入这个属性的话,我干嘛用这个框架上传文件啊- -

是一个很简单的例子,贴代码:

Jsp
HTML code
<%@ page language="java" contentType="text/html; charset=utf-8"    pageEncoding="utf-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>Insert title here</title><script src="scripts/jquery-1.4.2.js"></script><script src="scripts/ajaxfileupload.js"></script><script type="text/javascript">function uploadFile(){    $('#fileUpload').click();        $.ajaxFileUpload({            url: "ajax/fileUpload.action",            //data: {"fileRemak":"123"} ,    //$("#MyForm").serialize(),            secureuri:false,            fileElementId:'fileUpload',            dataType: 'json',            //type : "post" ,            success: function (msg) {                alert("上传附件成功!                      "+msg);                $('#fileUpload').val() ;                //$('#fileUpload').val('');            },            error: function () {                alert("异常,上传失败!");                //$('#fileUpload').val('');            }        });}</script></head><body >    <table align="center" border="1" cellpadding="0" cellspacing="1" class="formTableCore">        <tr>            <td class="formLabel" colspan="3">                <span id="b6">                    <input type="file" id="fileUpload" name="fileUpload" style="display:none;" />                    <input type="button" value="新增附件" class="formButton" onclick="uploadFile();"/>                </span>            </td>        </tr>    </table></body></html>


Action
Java code
package ajax;import java.io.File;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.http.HttpServletResponse;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionSupport;public class FileUpload{        public String fileRemak ;    public File fileUpload ;        public File getFileUpload() {        return fileUpload;    }    public void setFileUpload(File fileUpload) {        this.fileUpload = fileUpload;    }    public String getFileRemak() {        return fileRemak;    }    public void setFileRemak(String fileRemak) {        this.fileRemak = fileRemak;    }        public String fileUpload(){                        File copyFile = fileUpload ;                System.err.println("=================================");        System.err.println("=================================");        System.err.println("=================================");        System.err.println("=================================");        System.out.println("=================================");        System.out.println("=================================");                System.out.println(fileRemak);        System.out.println(copyFile==null ? "null":copyFile.getPath());        if(copyFile!=null){            System.out.println(copyFile.getPath());            System.out.println(copyFile.getName());            System.out.println(copyFile.getAbsolutePath());        }                System.err.println("=================================");        System.err.println("=================================");        System.err.println("=================================");        System.err.println("=================================");        System.out.println("=================================");        System.out.println("=================================");        System.out.println("=================================");                HttpServletResponse response = ServletActionContext.getResponse();        response.setCharacterEncoding("gb2312") ;                try {            PrintWriter out = response.getWriter() ;                                    out.print("哦...") ;                        out.flush() ;            out.close() ;        } catch (IOException e) {            e.printStackTrace();        }                return null ;    }}
  相关解决方案