zoukankan      html  css  js  c++  java
  • JQUERY插件JqueryAjaxFileUplaoder----更简单的异步文件上传

     

    异步上传相信大家都做过类似的功能,JqueryAjaxFileUploader为我们提供了更简单的实现和使用方式。不过既然是JQUERY的插件那么它所依赖的环境大家都懂得。JqueryAjaxFileUploader并不华丽,也没有提供美化文件上传控件的css,它并不像jQuery File Upload(喜欢的同学可以去尝试下),提供了美观的样式和专门的图片预览、多任务上传等等, JqueryAjaxFileUploader 所拥有的很简单,只是异步上传文件的功能,当然这并不排除由你亲自为它披上一件华丽的外衣。

    jQuery File Uplaod插件官方Demo:

    好了言归正传,让我们一起来看下 JqueryAjaxFileUplaoder 的使用步骤:

    Java代码 复制代码 收藏代码
    1. <html>   
    2.     <head>   
    3.         <base href="<%=basePath%>">   
    4.   
    5.         <title>My starting page</title>   
    6.         <meta http-equiv="pragma" content="no-cache">   
    7.         <meta http-equiv="cache-control" content="no-cache">   
    8.         <meta http-equiv="expires" content="0">   
    9.         <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">   
    10.         <meta http-equiv="description" content="This is my page">   
    11.         <script type="text/javascript" src="js/jquery.js">   
    12. </script>   
    13.         <script type="text/javascript" src="js/ajaxfileupload.js">   
    14. </script>   
    15.         <script type="text/javascript">   
    16. function ajaxFileUpload() {   
    17.   
    18.     $("#loading").ajaxStart(function() {   
    19.         $(this).show();   
    20.     })//开始上传文件时显示一个图片   
    21.     .ajaxComplete(function() {   
    22.         $(this).hide();   
    23.     });//文件上传完成将图片隐藏起来   
    24.   
    25.     $.ajaxFileUpload( {   
    26.         url : 'fileUpload.action',//用于文件上传的服务器端请求地址   
    27.         secureuri : false,//一般设置为false   
    28.         fileElementId : 'File',//文件上传控件的id属性   
    29.         dataType : 'json',//返回值类型 一般设置为json   
    30.         success : function(data, status) //服务器成功响应处理函数   
    31.         {   
    32.             alert(data.message);//从服务器返回的json中取出message中的数据,其中message为在struts2中定义的成员变量   
    33.             if (typeof (data.error) != 'undefined') {   
    34.                 if (data.error != '') {   
    35.                     alert(data.error);   
    36.                 } else {   
    37.                     alert(data.message);   
    38.                 }   
    39.             }   
    40.         },   
    41.         error : function(data, status, e)//服务器响应失败处理函数   
    42.         {   
    43.             alert(e);   
    44.         }   
    45.     })   
    46.     return false;   
    47. }   
    48. </script>   
    49.     </head>   
    50.   
    51.     <body>   
    52.         <img src="img/loading.gif" id="loading" style="display: none;">   
    53.         <input type="file" id="file" name="file" />   
    54.         <br />   
    55.         <input type="button" value="上传" onclick="return ajaxFileUpload();">   
    56.     </body>   
    57. </html>  
    <html>
    	<head>
    		<base href="<%=basePath%>">
    
    		<title>My starting page</title>
    		<meta http-equiv="pragma" content="no-cache">
    		<meta http-equiv="cache-control" content="no-cache">
    		<meta http-equiv="expires" content="0">
    		<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    		<meta http-equiv="description" content="This is my page">
    		<script type="text/javascript" src="js/jquery.js">
    </script>
    		<script type="text/javascript" src="js/ajaxfileupload.js">
    </script>
    		<script type="text/javascript">
    function ajaxFileUpload() {
    
    	$("#loading").ajaxStart(function() {
    		$(this).show();
    	})//开始上传文件时显示一个图片
    	.ajaxComplete(function() {
    		$(this).hide();
    	});//文件上传完成将图片隐藏起来
    
    	$.ajaxFileUpload( {
    		url : 'fileUpload.action',//用于文件上传的服务器端请求地址
    		secureuri : false,//一般设置为false
    		fileElementId : 'File',//文件上传控件的id属性
    		dataType : 'json',//返回值类型 一般设置为json
    		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>
    		<img src="img/loading.gif" id="loading" style="display: none;">
    		<input type="file" id="file" name="file" />
    		<br />
    		<input type="button" value="上传" onclick="return ajaxFileUpload();">
    	</body>
    </html>
     

    注意:引入JS文件不要搞错了顺序,一定是先引入jQuery再引入插件。否则后果的严重性你是可以想到的。

    上文中我请求了一个Action,并在Action里写好所需要的参数,以及文件格式的判断(这里我只是做的伪实现,过滤文件格式还请大家认真考虑),这个大家肯定都熟。

    Java代码 复制代码 收藏代码
    1. import java.io.File;   
    2. import java.io.FileInputStream;   
    3. import java.io.FileOutputStream;   
    4.   
    5. import org.apache.struts2.ServletActionContext;   
    6.   
    7. import com.opensymphony.xwork2.ActionSupport;   
    8.   
    9. @SuppressWarnings("serial")   
    10. public class FileAction extends ActionSupport {   
    11.   
    12.     private File file;   
    13.     private String fileFileName;   
    14.     private String fileFileContentType;   
    15.   
    16.     private String message = "你已成功上传文件";   
    17.        
    18.     public String getMessage() {   
    19.         return message;   
    20.     }   
    21.   
    22.     public void setMessage(String message) {   
    23.         this.message = message;   
    24.     }   
    25.   
    26.     public File getFile() {   
    27.         return file;   
    28.     }   
    29.   
    30.     public void setFile(File file) {   
    31.         this.file = file;   
    32.     }   
    33.   
    34.     public String getFileFileName() {   
    35.         return fileFileName;   
    36.     }   
    37.   
    38.     public void setFileFileName(String fileFileName) {   
    39.         this.fileFileName = fileFileName;   
    40.     }   
    41.   
    42.     public String getFileFileContentType() {   
    43.         return fileFileContentType;   
    44.     }   
    45.   
    46.     public void setFileFileContentType(String fileFileContentType) {   
    47.         this.fileFileContentType = fileFileContentType;   
    48.     }   
    49.   
    50.     @SuppressWarnings("deprecation")   
    51.     @Override  
    52.     public String execute() throws Exception {   
    53.            
    54.         String path = ServletActionContext.getRequest().getRealPath("/upload");   
    55.   
    56.         try {   
    57.             File f = this.getFile();   
    58.             if(this.getFileFileName().endsWith(".exe")){   
    59.                 message="对不起,你上传的文件格式不允许!!!";   
    60.                 return ERROR;   
    61.             }   
    62.             FileInputStream inputStream = new FileInputStream(f);   
    63.             FileOutputStream outputStream = new FileOutputStream(path + "/"+ this.getFileFileName());   
    64.             byte[] buf = new byte[1024];   
    65.             int length = 0;   
    66.             while ((length = inputStream.read(buf)) != -1) {   
    67.                 outputStream.write(buf, 0, length);   
    68.             }   
    69.             inputStream.close();   
    70.             outputStream.flush();   
    71.         } catch (Exception e) {   
    72.             e.printStackTrace();   
    73.             message = "对不起,文件上传竟然失败了!!!!";   
    74.         }   
    75.         return SUCCESS;   
    76.     }   
    77.   
    78. }   
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    
    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 {
            
            String path = ServletActionContext.getRequest().getRealPath("/upload");
    
            try {
                File f = this.getFile();
                if(this.getFileFileName().endsWith(".exe")){
                    message="对不起,你上传的文件格式不允许!!!";
                    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;
        }
    
    } 

    还有重要的一步,配置Struts配置文件,一定要注意的是Action中result的配置contentType参数是一定要有的,否则浏览器总是提示将返回的JSON结果另存为文件,不会交给ajaxfileupload处理。这是因为struts2 JSON Plugin默认的contentType为application/json,而ajaxfileupload则要求为text/html。

    Xml代码 复制代码 收藏代码
    1. <?xml version="1.0" encoding="UTF-8" ?>  
    2. <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">  
    3. <struts>  
    4.     <package name="struts2" extends="json-default">  
    5.         <action name="fileUpload" class="com.ajaxfile.action.FileAction">  
    6.             <result type="json" name="success">  
    7.                 <param name="contentType">  
    8.                     text/html   
    9.                 </param>  
    10.             </result>  
    11.             <result type="json" name="error">  
    12.                 <param name="contentType">  
    13.                     text/html   
    14.                 </param>  
    15.             </result>  
    16.         </action>  
    17.     </package>  
    18. </struts>     
  • 相关阅读:
    sql被注入,用友不能建账
    项目总帐金额翻倍
    1)123104科目的余额出现翻倍情况,经调数据库,期初余额已调平,但余额表中的数仍是未调平前的错误数。2)一月结账时提示有一科目119101的总账与个人明细账不平....
    尚有已全部暂估报销的单据未进行处理,不能进行12月的期末处理
    用友U8尚有已全部暂估报销的单据未进行处理,不能进行12月的期末处理
    用sql替换T6工作流中的操作员
    解决win7科迈登录报错RASRDP MODULE已停止工作
    sql2005 64 位 连接 sql2000 32位
    jquery选择器
    深入理解jQuery中$.get、$.post、$.getJSON和$.ajax的用法
  • 原文地址:https://www.cnblogs.com/felix-/p/4351089.html
Copyright © 2011-2022 走看看