上传文件

function upExcel(moduleid, positionid, teamroleid) {
var html = "<form id='file_form' action='attence!upAttenceExcel.do' enctype='multipart/form-data'"
+ "method='post'>"
+ "<table width='300' class='tablesorter' border='0' " +
"cellpadding='2' cellspacing='1'  align='center' >"
+ "<tr bgcolor='#ffffff' height='22'>"
+ "<td>"
+ "excel文件:"
+ "</td>"
+ "<td>"
+ "<input type='file' name='upload' id='file_input' />"
+ "</td>"
+ "<td>"
+ "<input type='hidden' name='moduleId' value='"+moduleid+"'/>"
+ "<input type='hidden' name='positionId' value='"+positionid+"'/>"
+ "<input type='hidden' name='teamroleId' value='"+teamroleid+"' />"
+ "</td>" + "</tr>" + "</table>" + "</form>"


art.dialog({
content : html,
ok : function() {


var fileName = $('#file_input').val();
if (fileName === '') {
alert('请选择文件');
return false;
}
var fileType = (fileName.substring(
fileName.lastIndexOf(".") + 1, fileName.length))
.toLowerCase();
if (fileType !== 'xls' && fileType !== 'xlsx') {
alert('文件格式不正确,请选择excel文件!');
return false;
}
$("#file_form").submit();


/*   $("#file_form").ajaxSubmit({
        dataType : "json",
        success : function(data, textStatus) {
            if (data['result'] === 'OK') {
                console.log('上传文件成功');
            } else {
                console.log('文件格式错误');
            }
            return false;
        }
    }); */
}
});


}


/**
         * 将上传的excel存到upload目录下
         */
        this.struts2Util.getRequest().setCharacterEncoding("utf-8");
        File saved = new File(ServletActionContext.getServletContext().getRealPath("upload"), uploadFileName);


        InputStream ins = null;
        OutputStream ous = null;
        try {
            saved.getParentFile().mkdirs(); // 确保文件夹upload存在
            ins = new FileInputStream(upload); // 读取临时文件
            ous = new FileOutputStream(saved); // 写入到upload下
            byte[] b = new byte[1024];
            int len = 0;
            while ((len = ins.read(b)) != -1) {
                ous.write(b, 0, len);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (ous != null)
                ous.close();
            if (ins != null)
                ins.close();


        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值