uploadify多文件上传

一,后台文件处理类(struts框架)

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.stereotype.Controller;
import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
@Controller
@ParentPackage("default")
@Namespace("/")
@Action(value = "upload", results = {
         @Result(name = "main", location = "main.html")
})
public class FileUploadAction extends ActionSupport{
    private File videoObj;
    private String videoObjFileName;
    private String videoObjContentType;
   
    private String filename;
    private String upload;
   
    @Override
    public String execute() {
        try {
            String fix = videoObjFileName.substring(videoObjFileName.lastIndexOf("."), videoObjFileName.length());
            String descPath = "D:\\test_upload_files\\"+Long.toString(new Date().getTime())+fix;
            FileUtils.copyFile(videoObj,new File(descPath) );
            setResponseFlag(-1);
            setResponseMsg("22");
            writeResponse(true);
        } catch (Exception e) {
           
        }
        return null;
    }

   
    public File getVideoObj() {
        return videoObj;
    }

    public void setVideoObj(File videoObj) {
        this.videoObj = videoObj;
    }

    public String getVideoObjFileName() {
        return videoObjFileName;
    }

    public void setVideoObjFileName(String videoObjFileName) {
        this.videoObjFileName = videoObjFileName;
    }

    public String getVideoObjContentType() {
        return videoObjContentType;
    }

    public void setVideoObjContentType(String videoObjContentType) {
        this.videoObjContentType = videoObjContentType;
    }


    public String getFilename() {
        return filename;
    }


    public void setFilename(String filename) {
        this.filename = filename;
    }


    public String getUpload() {
        return upload;
    }


    public void setUpload(String upload) {
        this.upload = upload;
    }

}

二,页面代码如下:

<!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.7.js"></script>
<script src="uploadify/jquery.uploadify.js" type="text/javascript"></script>
<link rel="stylesheet" type="text/css" href="uploadify/uploadify.css">
<script type="text/javascript">
$(function() {
$('#file_upload').uploadify({
auto:false,
fileObjName:'videoObj',
removeCompleted:false,
fileSizeLimit:'300000',//500M
//fileTypeExts:'*.mp4',//'*.*' 这个参数只能限制为一种类型,不能与下面的fileType同时使用,除非只有一种类型 
//fileType:'mp4,swf',//自定义文件类型 
//uploadLimit:10,//上传文件个数
swf : 'uploadify/uploadify.swf',
uploader : 'upload',
onUploadSuccess : onUploadSuccess
});
$("#file_upload-queue .cancel a").live("click",function(){
var divId = $(this).parent().parent().attr("id");
var fileId = $("#"+divId).find("input").attr("id");
if(typeof(fileId)=='undefined'){
if(confirm("确定要删除么?")){
$("#"+divId).fadeOut(500, function() {
$("#"+divId).remove();
});
$("#file_upload").uploadify('cancel', divId);
}
}else{
if(confirm("已经上传至服务器了,确定要删除么?")){
alert("发送请求删除,,再移除掉!");
$("#"+divId).fadeOut(500, function() {
$("#"+divId).remove();
});
$("#file_upload").uploadify('cancel', divId);
}
}
});
});
function upload(){
$('#file_upload').uploadify('upload', '*');
}
function stop(){
$('#file_upload').uploadify('stop', '*');
}
function onUploadSuccess(file, data, response){
//上传完成之后,调用的方法,可以获取从后台传递过来的数据(data)
//alert(jQuery.parseJSON(data).result);
//response == true;
//alert(file.id+"__"+response);
var fileId = file.id;
$("#"+fileId+" input").attr("id","file_id_"+fileId);
}

</script>
</head>
<body>
<h1>Uploadify Demo</h1>
<div id="queue"></div>
<input id="file_upload" type="file" multiple="true">
<input type="button" value="start upload..." οnclick="upload();">
<input type="button" value="stop upload..." οnclick="stop();">
</body>
</html>


三,源码下载




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值