jquery ajaxfileupload異步上傳插件
本文實(shí)例為大家分享了ajaxfileupload異步上傳插件的使用方法,供大家參考,具體內(nèi)容如下
服務(wù)器端采用struts2來處理文件上傳。
所需環(huán)境:
jquery.js
ajaxfileupload.js
struts2所依賴的jar包
及struts2-json-plugin-2.1.8.1.jar
編寫文件上傳的Action
package com.ajaxfile.action; 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.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts> <package name="struts2" extends="json-default"> <action name="fileUploadAction" class="com.ajaxfile.action.FileAction"> <result type="json" name="success"> <param name="contentType"> text/html </param> </result> <result type="json" name="error"> <param name="contentType"> text/html </param> </result> </action> </package> </struts>
注意結(jié)合Action觀察struts.xml中result的配置。
contentType參數(shù)是一定要有的,否則瀏覽器總是提示將返回的JSON結(jié)果另存為文件,不會交給ajaxfileupload處理。這是因?yàn)閟truts2 JSON Plugin默認(rèn)的contentType為application/json,而ajaxfileupload則要求為text/html。
文件上傳的jsp頁面
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> <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:'fileUploadAction.action',//用于文件上傳的服務(wù)器端請求地址 secureuri:false,//一般設(shè)置為false fileElementId:'file',//文件上傳空間的id屬性 <input type="file" id="file" name="file" /> dataType: 'json',//返回值類型 一般設(shè)置為json success: function (data, status) //服務(wù)器成功響應(yīng)處理函數(shù) { alert(data.message);//從服務(wù)器返回的json中取出message中的數(shù)據(jù),其中message為在struts2中action中定義的成員變量 if(typeof(data.error) != 'undefined') { if(data.error != '') { alert(data.error); }else { alert(data.message); } } }, error: function (data, status, e)//服務(wù)器響應(yīng)失敗處理函數(shù) { alert(e); } } ) return false; } </script> </head> <body> <img src="loading.gif" id="loading" style="display: none;"> <input type="file" id="file" name="file" /> <br /> <input type="button" value="上傳" onclick="return ajaxFileUpload();"> </body> </html>
注意觀察<body>中的代碼,并沒有form表單。只是在按鈕點(diǎn)擊的時候觸發(fā)ajaxFileUpload()方法。需要注意的是js文件引入的先后順序,ajaxfileupload.js依賴于jquery因此你知道的。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- jquery ajaxfileupload異步上傳插件使用詳解
- jQuery插件ajaxFileUpload異步上傳文件
- PHP結(jié)合jQuery插件ajaxFileUpload實(shí)現(xiàn)異步上傳文件實(shí)例
- jquery中的ajax異步上傳
- jQuery異步上傳文件插件ajaxFileUpload詳細(xì)介紹
- JQuery插件ajaxfileupload.js異步上傳文件實(shí)例
- jQuery插件ajaxFileUpload實(shí)現(xiàn)異步上傳文件效果
- jquery的ajaxSubmit()異步上傳圖片并保存表單數(shù)據(jù)演示代碼
- jquery之a(chǎn)jaxfileupload異步上傳插件(附工程代碼)
- jquery+ajax實(shí)現(xiàn)異步上傳文件顯示進(jìn)度條
相關(guān)文章
jquery+CSS實(shí)現(xiàn)的多級豎向展開樹形TRee菜單效果
這篇文章主要介紹了jquery+CSS實(shí)現(xiàn)的多級豎向展開樹形TRee菜單效果,通過jquery自定義函數(shù)設(shè)置相應(yīng)參數(shù)實(shí)現(xiàn)屬性TRee菜單效果的功能,非常具有實(shí)用價值,需要的朋友可以參考下2015-08-08JS 遍歷 json 和 JQuery 遍歷json操作完整示例
這篇文章主要介紹了JS 遍歷 json 和 JQuery 遍歷json操作,結(jié)合完整實(shí)例形式詳細(xì)分析了JavaScript與jQuery遍歷json格式數(shù)據(jù)的簡單實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-11-11jQuery圖片輪播(二)利用構(gòu)造函數(shù)和原型創(chuàng)建對象以實(shí)現(xiàn)繼承
本文主要介紹了利用構(gòu)造函數(shù)和原型創(chuàng)建對象以實(shí)現(xiàn)繼承,并附上完成簡單輪播對象的封裝的示例代碼。有興趣的朋友可以看下2016-12-12Jquery 組合form元素為json格式,asp.net反序列化
Jquery組合form元素為json格式,asp.net反序列化實(shí)現(xiàn)代碼,大家可以具體的看下面的說明。2009-07-07JQuery Dialog對話框 不能通過Esc關(guān)閉的原因分析及解決辦法
這篇文章主要介紹了JQuery Dialog對話框 不能通過Esc關(guān)閉的原因分析及解決辦法,需要的朋友可以參考下2017-01-01jQuery動畫顯示和隱藏效果實(shí)例演示(附demo源碼下載)
這篇文章主要介紹了jQuery動畫顯示和隱藏效果實(shí)現(xiàn)方法,并附帶了demo源碼供讀者下載參考,涉及jQuery操作圖片的顯示,隱藏及淡入淡出等效果,需要的朋友可以參考下2015-12-12jQuery刪除節(jié)點(diǎn)的三個方法即remove()detach()和empty()
jQuery提供了三種刪除節(jié)點(diǎn)的方法,即remove(),detach()和empty(),下面為大家詳細(xì)介紹下jQuery刪除節(jié)點(diǎn)三個方法的具體使用2013-12-12