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>  

    注意:引入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. }   

    还有重要的一步,配置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>      

     Ok就是这样,要动手的同学马上Coding吧。顺便提供下JqueryAjaxFileUploader的JS文件。

  • 相关阅读:
    一个极好的ALV例子
    ABAP中读取文本文件(TXT DOCUMENT)并用ALV显示
    SMARTFORM报表程序设计(3)
    FOR ALL ENTRIES IN
    SMARTFORM报表程序设计(2)
    SMARTFORM报表程序设计(1)
    ABAP用FILE_OPEN_DIALOG打开多个文件后打印出名称
    LaTeX 表格的处理 [转]
    ubuntu安装deb,rpm安装包,tar.gz的方法 [转]
    OpenGL学习入门之VS2010环境配置 [转]
  • 原文地址:https://www.cnblogs.com/ranzige/p/3816464.html
Copyright © 2011-2022 走看看