日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

JQUERY插件JqueryAjaxFileUplaoder----更简单的异步文件上传

發布時間:2023/12/18 编程问答 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 JQUERY插件JqueryAjaxFileUplaoder----更简单的异步文件上传 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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

?

jQuery File Uplaod插件官方Demo:

好了言歸正傳,讓我們一起來看下 JqueryAjaxFileUplaoder?的使用步驟:

Java代碼??
  • <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="上傳"?οnclick="return?ajaxFileUpload();">??
  • ????</body>??
  • </html>??
  • ?

    注意:引入JS文件不要搞錯了順序,一定是先引入jQuery再引入插件。否則后果的嚴重性你是可以想到的。

    上文中我請求了一個Action,并在Action里寫好所需要的參數,以及文件格式的判斷(這里我只是做的偽實現,過濾文件格式還請大家認真考慮),這個大家肯定都熟。

    ?

    Java代碼??
  • 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。

    ?

    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="fileUpload"?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>??????
  • ?Ok就是這樣,要動手的同學馬上Coding吧。順便提供下JqueryAjaxFileUploader的JS文件。

    ?

    轉載于:https://www.cnblogs.com/ranzige/p/3816464.html

    總結

    以上是生活随笔為你收集整理的JQUERY插件JqueryAjaxFileUplaoder----更简单的异步文件上传的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。