AjaxFileUpload 上傳圖片 後台

JS

   function upload_files() {
    var fileValue = $("#file").val();
    if(fileValue == ""){
        var msg ='<s:property value="getText('selectFile')"  escape="false"/>';
        setFlatMsg({'msg':msg,'backColor':'#FEFF99'});
        return false;
    }else{
    var aa = fileValue.lastIndexOf(".");
        var typeName = (fileValue.substr(fileValue.lastIndexOf(".") + 1)).toLowerCase();
        if (typeName == "txt" ){
        $(document).ajaxStart(function() {
            $("#loading").show();
        }).ajaxComplete(function() {
            $("#loading").hide();
        });
        //var elementIds=["flag"]; //flag为id、name属性名
        $.ajaxFileUpload({
            url : 'a-dataDetailsUpload?uid=${attendanceData.attendanceDataId}',
            secureuri : false,
            fileElementId : 'file',
            dataType : 'text',//返回值类型,一般设置为json、application/json
            //elementIds: elementIds, //传递参数到服务器
            success : function(data, status) {
                var repObj = $.parseJSON(data);
                if (repObj.data == 'ok') {
                    var msg='<s:property value="getText('uploadSuccess')"  escape="false"/>';
                    setFlatMsg({'msg':msg});
                    //alert("导入成功!");
                } else {
                    var msg='<s:property value="getText('uploadFail')"  escape="false"/>';
                    setFlatMsg({'msg':msg,'backColor':'#FEFF99'});
                    //alert("数据异常!");
                }
            },
            error : function(data, status, e) {
                alert(e);
            }
        });
                    }else{
                var msg='<s:property value="getText('uploadWrongFileType')"  escape="false"/>';
                    setFlatMsg({'msg':msg,'backColor':'#FEFF99'});
            }
    }
    }



Action

    public void dataDetailsUpload(){

        try {
            ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
            String path = ServletActionContext.getRequest().getRealPath("/upload");  
            //String path = org.apache.struts2.ServletActionContext.getServletContext().getContextPath();
            try {  
                File f = this.getFile();  
                if(!this.getFileFileName().endsWith(".txt")){  
                    ServletActionContext.getResponse().getWriter().print("{\"data\":\"ng\"}");
                    return;
//                    return "txtReturn";  
                }  
                FileInputStream inputStream = new FileInputStream(f);  
                FileOutputStream outputStream = new FileOutputStream(path + "/"+ fileFileName);  
                byte[] buf = new byte[1024];  
                int length = 0;  
                while ((length = inputStream.read(buf)) != -1) {  
                    outputStream.write(buf, 0, length);  
                }  
                inputStream.close();  
                outputStream.flush();  
                ServletActionContext.getResponse().getWriter().print("{\"data\":\"ok\"}");
                
            } catch (Exception e) {  
                e.printStackTrace();  
                ServletActionContext.getResponse().getWriter().print("{\"data\":\"ng\"}");
            }  
        } catch (Exception e) {
            e.printStackTrace();
//            return "txtReturn";
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值