異步上傳相信你們都作過相似的功能,JqueryAjaxFileUploader爲咱們提供了更簡單的實現和使用方式。不過既然是JQUERY的插件那麼它所依賴的環境你們都懂得。JqueryAjaxFileUploader並不華麗,也沒有提供美化文件上傳控件的css,它並不像jQuery File Upload(喜歡的同窗能夠去嘗試下),提供了美觀的樣式和專門的圖片預覽、多任務上傳等等, JqueryAjaxFileUploader 所擁有的很簡單,只是異步上傳文件的功能,固然這並不排除由你親自爲它披上一件華麗的外衣。javascript
jQuery File Uplaod插件官方Demo:css
好了言歸正傳,讓咱們一塊兒來看下 JqueryAjaxFileUplaoder 的使用步驟: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再引入插件。不然後果的嚴重性你是能夠想到的。java
上文中我請求了一個Action,並在Action裏寫好所須要的參數,以及文件格式的判斷(這裏我只是作的僞實現,過濾文件格式還請你們認真考慮),這個你們確定都熟。jquery
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。ajax