日期:2014-05-17  浏览次数:20748 次

$.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("