jquery之a(chǎn)jaxfileupload異步上傳插件(附工程代碼)
更新時(shí)間:2013年04月17日 16:01:04 作者:
在處理文件上傳時(shí)需要使用到文件的異步上傳,這里使用Jquery Ajax File Uploader這個(gè)組件,服務(wù)器端采用struts2來(lái)處理文件上傳
點(diǎn)我下載工程代碼
由于項(xiàng)目需求,在處理文件上傳時(shí)需要使用到文件的異步上傳。這里使用Jquery Ajax File Uploader這個(gè)組件下載地址:http://www.phpletter.com/download_project_version.php?version_id=6
服務(wù)器端采用struts2來(lái)處理文件上傳。
所需環(huán)境:
jquery.js
ajaxfileupload.js
struts2所依賴的jar包
及struts2-json-plugin-2.1.8.1.jar
編寫(xiě)文件上傳的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="對(duì)不起,你上傳的文件格式不允許!!!";
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 = "對(duì)不起,文件上傳失敗了!!!!";
}
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é)果另存為文件,不會(huì)交給ajaxfileupload處理。這是因?yàn)閟truts2 JSON Plugin默認(rèn)的contentType為application/json,而ajaxfileupload則要求為text/html。
文件上傳的jsp頁(yè)面
<%@ 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();
})//開(kāi)始上傳文件時(shí)顯示一個(gè)圖片
.ajaxComplete(function(){
$(this).hide();
});//文件上傳完成將圖片隱藏起來(lái)
$.ajaxFileUpload
(
{
url:'fileUploadAction.action',//用于文件上傳的服務(wù)器端請(qǐng)求地址
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>中的代碼,并沒(méi)有form表單。只是在按鈕點(diǎn)擊的時(shí)候觸發(fā)ajaxFileUpload()方法。需要注意的是js文件引入的先后順序,ajaxfileupload.js依賴于jquery因此你知道的。
點(diǎn)我下載工程代碼
由于項(xiàng)目需求,在處理文件上傳時(shí)需要使用到文件的異步上傳。這里使用Jquery Ajax File Uploader這個(gè)組件下載地址:http://www.phpletter.com/download_project_version.php?version_id=6
服務(wù)器端采用struts2來(lái)處理文件上傳。
所需環(huán)境:
jquery.js
ajaxfileupload.js
struts2所依賴的jar包
及struts2-json-plugin-2.1.8.1.jar
編寫(xiě)文件上傳的Action
復(fù)制代碼 代碼如下:
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="對(duì)不起,你上傳的文件格式不允許!!!";
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 = "對(duì)不起,文件上傳失敗了!!!!";
}
return SUCCESS;
}
}
struts.xml
復(fù)制代碼 代碼如下:
<?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é)果另存為文件,不會(huì)交給ajaxfileupload處理。這是因?yàn)閟truts2 JSON Plugin默認(rèn)的contentType為application/json,而ajaxfileupload則要求為text/html。
文件上傳的jsp頁(yè)面
復(fù)制代碼 代碼如下:
<%@ 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();
})//開(kāi)始上傳文件時(shí)顯示一個(gè)圖片
.ajaxComplete(function(){
$(this).hide();
});//文件上傳完成將圖片隱藏起來(lái)
$.ajaxFileUpload
(
{
url:'fileUploadAction.action',//用于文件上傳的服務(wù)器端請(qǐng)求地址
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>中的代碼,并沒(méi)有form表單。只是在按鈕點(diǎn)擊的時(shí)候觸發(fā)ajaxFileUpload()方法。需要注意的是js文件引入的先后順序,ajaxfileupload.js依賴于jquery因此你知道的。
點(diǎn)我下載工程代碼
您可能感興趣的文章:
- jquery ajaxfileupload異步上傳插件
- 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+ajax實(shí)現(xiàn)異步上傳文件顯示進(jìn)度條
相關(guān)文章
JS+CSS實(shí)現(xiàn)圖片預(yù)加載與背景圖上中下切圖
這篇文章介紹了JS+CSS實(shí)現(xiàn)圖片預(yù)加載與背景圖上中下切圖的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-06-06超好用的jQuery分頁(yè)插件jpaginate用法示例【附源碼下載】
這篇文章主要介紹了超好用的jQuery分頁(yè)插件jpaginate用法,結(jié)合實(shí)例形式簡(jiǎn)單分析了jQuery分頁(yè)插件jpaginate的基本調(diào)用方式、參數(shù)屬性及配置方法,并附帶源碼供讀者下載,需要的朋友可以參考下2018-12-12利用jQuery及AJAX技術(shù)定時(shí)更新GridView的某一列數(shù)據(jù)
這篇文章主要介紹了利用jQuery及AJAX技術(shù)定時(shí)更新GridView的某一列數(shù)據(jù)的方法,這里的GridView是指C#軟件開(kāi)發(fā)中的GridView控件,需要的朋友可以參考下2015-12-12jQuery登陸判斷簡(jiǎn)單實(shí)現(xiàn)代碼
登陸判斷在實(shí)際應(yīng)用中還是比較常見(jiàn),在客戶端執(zhí)行判斷可以減少服務(wù)器端的負(fù)擔(dān),感興趣的朋友可以參考下2013-04-04jquery垂直公告滾動(dòng)實(shí)現(xiàn)代碼
公告滾動(dòng)想必大家都有見(jiàn)到過(guò)吧,實(shí)現(xiàn)方法也有很多,下面為大家介紹使用jquery實(shí)現(xiàn)垂直公告滾動(dòng),感興趣的朋友不要錯(cuò)過(guò)2013-12-12jQuery插件開(kāi)發(fā)基礎(chǔ)簡(jiǎn)單介紹
jquery插件開(kāi)發(fā)基礎(chǔ):開(kāi)發(fā)jQuery 插件的基本格式,開(kāi)發(fā)全局函數(shù)的基本格式,接下來(lái)為您詳細(xì)介紹,感興趣的朋友可以了解哦2013-01-01jquery ajax請(qǐng)求方式與提示用戶正在處理請(qǐng)稍等
為了提高用戶體驗(yàn)度,我們通常會(huì)給出 “正在處理,請(qǐng)稍等!”諸如此類的提示。我們可通過(guò)設(shè)置$.ajax()下的參數(shù)beforeSend()來(lái)實(shí)現(xiàn)2014-09-09