您的位置:首页 > Web前端 > JavaScript

ajaxFileupload.js

2013-07-20 15:32 225 查看
ajaxFileUpload上传多文件需要改源代码ajaxFileUpload.js,而且参数data上传到后台无数据。。。。

ajaxFileUpload.jsp

<%@ 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 type="text/javascript" src="js/jquery-1.8.2.js"></script>

<script type="text/javascript" src="js/index.js"></script>

<script type="text/javascript" src="js/ajaxfileupload.js"></script>

<script type="text/javascript">

function ajaxFileUpload()

{

//校验文件格式

var tmpFileValue = $("#file").val();

if(!/^.*?\.(gif|png|jpg|jpeg|bmp|doc|docx|xls|pdf|txt|mp3)$/.test(tmpFileValue.toLowerCase())){

alert("只能上传jpg、jpeg、png、bmp、gif、doc、docx、xls、pdf、txt、mp3格式的图片!");

return false;

}

var data = { username: 'my name'};

$("#loading")

.ajaxStart(function(){

$(this).show();

})//开始上传文件时显示一个图片

.ajaxComplete(function(){

$(this).hide();

});//文件上传完成将图片隐藏起来

$.ajaxFileUpload

(

{

url:'ajax/ajaxFileUploadAction.action',//用于文件上传的服务器端请求地址

type:'post',

secureuri:false,//一般设置为false

fileElementId:'file',//文件上传空间的id属性 <input type="file" id="file" name="file" />

dataType: 'json',//返回值类型 一般设置为json

data:data,
//带参数传递,有问题。。后台接受不到

//data:{"username": $("#username").val()},

success: function (data, status) //服务器成功响应处理函数

{

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

if(typeof(data.error) != 'undefined')

{

if(data.error != '')

{

alert(data.error);

}else

{

alert(data.message);

}

}

},

error: function (data, status, e)//服务器响应失败处理函数

{

alert(e);

}

}

)

return false;

}

</script>

</head>

<body>

<input type="text" name="username" id="username"/><br/>

<img src="loading.gif" id="loading" style="display: none;">

<input type="file" id="file" name="file" />

<br />

<input type="button" value="上传" onclick="return ajaxFileUpload();">

</body>

</html>

AjaxFileAction

package com.pb.cdjj.zxk.uploadAction;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.Action;

import com.opensymphony.xwork2.ActionSupport;

import com.pb.cdjj.zxk.entity.User;

public class AjaxFileAction extends ActionSupport {

private File file;

private String fileFileName;

private String fileFileContentType;

private String uploadFilePath;

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

private String username;

public String getUsername() {

return username;

}

public void setUsername(String username) {

this.username = username;

}

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;

}

public String getUploadFilePath() {

return uploadFilePath;

}

public void setUploadFilePath(String uploadFilePath) {

this.uploadFilePath = uploadFilePath;

}

public String execute() throws Exception {

System.out.println("username-------->" + username);

System.out.println("file-------->" + file);

System.out.println("file.length()-------->" + file.length());

String path = ServletActionContext.getServletContext().getRealPath(getUploadFilePath());

System.out.println(path);

try {

File f = this.getFile();

if(this.getFileFileName().endsWith(".exe")){

message="对不起,你上传的文件格式不允许!!!";

return Action.ERROR;

}

File newFile = new File(path + "/"+ this.getFileFileName());

if (!newFile.getParentFile().exists()) {

System.out.println("目录不存在,新建上传目录");

newFile.getParentFile().mkdir();

}

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.close();

} catch (Exception e) {

e.printStackTrace();

message = "对不起,文件上传失败了!!!!";

}

return Action.SUCCESS;

}

}

struts.xml

<!--演示ajaxFileUpload请求 -->

<action name="ajaxFileUploadAction" class="com.pb.cdjj.zxk.uploadAction.AjaxFileAction">

<param name="uploadFilePath">/upload</param>

<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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: