首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

jquery的ajaxfileupload插件实现异步上传的有关问题

2013-11-09 
jquery的ajaxfileupload插件实现异步上传的问题点击【上传】按钮加载图片一直显示,后台FileAction类未执行,

jquery的ajaxfileupload插件实现异步上传的问题
点击【上传】按钮加载图片一直显示,后台FileAction类未执行,请问是哪里的问题?

引入的js
<script src="/customerInfo/static/jquery/jquery-1.8.3.js" type="text/javascript"></script>
<script src="/customerInfo/static/AjaxFileUploader/ajaxfileupload.js" type="text/javascript"></script>


customerForm.jsp对应的代码:
1、提交文件控件的代码:
<div class="control-group">
  <label class="control-label">照片:</label>
  <div class="controls">
<input id="fileToUpload" type="file" size="45" name="fileToUpload" />
<img id="loading" src="${ctx}/static/images/loading.gif"style="display:none;">
        &nbsp;&nbsp;          <input type="button" id="buttonUpload" value="上传" 
         onclick="return ajaxFileUpload();" />
  </div>
</div>
2、javaScript里function的代码:
function ajaxFileUpload() {
$("#loading").ajaxStart(function() {
$(this).show();
})//开始上传文件时显示一个图片
.ajaxComplete(function() {
$(this).hide();
});//文件上传完成将图片隐藏起来

$.ajaxFileUpload({

url : 'fileUploadAction.action',//用于文件上传的服务器端请求地址
secureuri : false,//一般设置为false
fileElementId : 'fileToUpload',//文件上传空间的id属性 <input id="fileToUpload" type="file" size="45" name="fileToUpload"/>
dataType : 'json',//返回值类型 一般设置为json
success : function(data, status) //服务器成功响应处理函数
{

alert(data.message);//从服务器返回的json中取出message中的数据,其中message为在struts2中action中定义的成员变量

if (typeof (data.error) != 'undefined') {
if (data.error != '') {
alert(data.error);
} else {
alert(data.message);
}
}
},
error : function(data, status, e)//服务器响应失败处理函数
{
alert(e);
}
})

return false;

}

3、structs.xml文件的代码:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
    <package name="struts2" extends="json-default">
        <action name="fileUploadAction" class="com.harvest.customerInfor.common.utils.FileAction">
            <result type="json" name="success">
                <param name="contentType">
                    text/html
                </param>
            </result>
            <result type="json" name="error">
                <param name="contentType">
                    text/html
                </param>
            </result>
        </action>
    </package>
</struts>

4、类FileAction的代码
package com.harvest.customerInfor.common.utils;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Arrays;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class FileAction extends ActionSupport {

private File file;
private String fileFileName;
private String fileFileContentType;

private String message = "你已成功上传文件";

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}

public File getFile() {
return file;
}

public void setFile(File file) {
this.file = file;
}

public String getFileFileName() {
return fileFileName;
}

public void setFileFileName(String fileFileName) {
this.fileFileName = fileFileName;
}

public String getFileFileContentType() {
return fileFileContentType;
}

public void setFileFileContentType(String fileFileContentType) {
this.fileFileContentType = fileFileContentType;
}



@SuppressWarnings("deprecation")
@Override
public String execute() throws Exception {
System.out.println("------------execute");
String path = ServletActionContext.getRequest().getRealPath("/upload");
String[] fileTypes = new String[]{"gif","jpg","jpeg","png","bmp"};

try {
File f = this.getFile();
 String fileExt = this.getFileFileName().substring(this.getFileFileName().lastIndexOf(".") + 1).toLowerCase();
/*if(this.getFileFileName().endsWith(".exe")){
message="对不起,你上传的文件格式不允许!!!";
return ERROR;
}*/
if (!Arrays.<String> asList(fileTypes).contains(fileExt)) {
message = "只能上传 gif,jpg,jpeg,png,bmp等格式的文件!";
return ERROR;
}
 
FileInputStream inputStream = new FileInputStream(f);
FileOutputStream outputStream = new FileOutputStream(path + "/"+ this.getFileFileName());
byte[] buf = new byte[1024];
int length = 0;
while ((length = inputStream.read(buf)) != -1) {
outputStream.write(buf, 0, length);
}
inputStream.close();
outputStream.flush();
} catch (Exception e) {
e.printStackTrace();
message = "对不起,文件上传失败了!!!!";
}
return SUCCESS;
}

}

jquery ajaxfileupload 异步 文件上传
[解决办法]
这个用表单 ajax方式提交就行了 ,
[解决办法]
var options = {
success:function(responseText, statusText, xhr, $form){
  switch(responseText){
      case 'success' :  成功代码; break;
      default : 失败代码;
  }
}};
$(dataform).ajaxSubmit(options);

这是表单以ajax 提交 ,jquery的

后台就和正常的表单提交一样处理就行了。一般就用个流,接收表单中的流就可以了 ,之后流写到文件
[解决办法]
你的 url : 'fileUploadAction.action',   这个前面加上路径试试看看呢。 
[解决办法]
url : 'fileUploadAction'
[解决办法]
struts的定义的后缀是.action?
是的话去掉试试
url : 'fileUploadAction' 

热点排行