JQUERY插件JqueryAjaxFileUplaoder----更簡單的異步文件上傳

異步上傳相信你們都作過相似的功能,JqueryAjaxFileUploader爲咱們提供了更簡單的實現和使用方式。不過既然是JQUERY的插件那麼它所依賴的環境你們都懂得。JqueryAjaxFileUploader並不華麗,也沒有提供美化文件上傳控件的css,它並不像jQuery File Upload(喜歡的同窗能夠去嘗試下),提供了美觀的樣式和專門的圖片預覽、多任務上傳等等, JqueryAjaxFileUploader 所擁有的很簡單,只是異步上傳文件的功能,固然這並不排除由你親自爲它披上一件華麗的外衣。javascript

 

jQuery File Uplaod插件官方Demo:css

好了言歸正傳,讓咱們一塊兒來看下 JqueryAjaxFileUplaoder 的使用步驟:html

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再引入插件。不然後果的嚴重性你是能夠想到的。java

上文中我請求了一個Action,並在Action裏寫好所須要的參數,以及文件格式的判斷(這裏我只是作的僞實現,過濾文件格式還請你們認真考慮),這個你們確定都熟。jquery

 

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。ajax

 

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文件。apache

相關文章
相關標籤/搜索