Struts2+jquery.form.js實現圖片與檔案上傳的方法_jquery

來源:互聯網
上載者:User

本文執行個體講述了Struts2+jquery.form.js實現圖片與檔案上傳的方法。分享給大家供大家參考,具體如下:

jquery.form.js是jQuery的一個官方用語支援非同步上傳檔案的外掛程式。官方網站:http://plugins.jquery.com/form/

結合Struts2三步輕鬆實現檔案上傳

一般是針對一個頁面可能不止一個Form表單,所以在一個面提交表單會影響到另一個表單,為此,圖片上傳表單就可以使用無重新整理提交方式上傳,也就是非同步上傳,這時jquery.from.js就派上用場了。

一、HTML

匯入本jS到頁面、寫好上傳表單

<script type="text/javascript" src="/js/jquery.form.js"></script><!—圖片上傳 --><s:form id="picForm" name="picForm" action="/notice/showAddNotice.action" method="post"   enctype="multipart/form-data">    <input type="file" name="pic" size="30"/><input type="submit" value="上傳"/></s:form>

二、JS

// 為表單綁定非同步上傳的事件$(function(){    // 為表單綁定非同步上傳的事件    $("#picForm").ajaxForm({    url : "${pageContext.request.contextPath}/notice/uploadPic.action", // 請求的url    type : "post", // 請求方式    dataType : "text", // 響應的資料類型    async :true, // 非同步    success : function(imageUrl){        //alert(imageUrl);    },    error : function(){        alert("資料載入失敗!");    }});// 為提交按鈕綁定事件$("#saveBtn").click(function(){    // 表單輸入較驗    var title = $("#title");    // 擷取textarea的內容    var content = tinyMCE.get('content').getContent();    var msg = "";    if ($.trim(title.val()) == ""){        msg = "公告標題不可為空!";        title.focus();    }else if ($.trim(content) == ""){        msg = "內容不可為空!";    }    msg = "";    if (msg != ""){        alert(msg);    }else{        // 表單提交        $("#noticeForm").submit();    }});

三、Struts2Action

public class uploadPicAjax extends AbstractAjaxAction {    private static final long serialVersionUID = -4742151106080093662L;    /** Struts2檔案上傳的三個屬性 */    private File pic;    private String picFileName;    private String picContentType;    @Override    protected String getJson() throws Exception {        //擷取項目部署的路徑        String realPath = ServletActionContext.getServletContext()                .getRealPath("/images/notice");        //產生新的檔案名稱        String newFileName = UUID.randomUUID().toString()+"."                +FilenameUtils.getExtension(picFileName);// 擷取檔案的尾碼名 aa.jpg --> jpg        FileUtils.copyFile(pic, new File(realPath + File.separator + newFileName));        return "/images/notice/" + newFileName;    }    /** setter and getter method **/    ......}

四、配置Struts2.xml

<!-- 圖片的非同步上傳 --><action name="uploadPic" class="com.wise.hrm.action.notice.uploadPicAjax"></action>

好了,從頁面到後台就已經寫完了。這樣就可以上傳了。完畢!

更多關於jQuery相關內容感興趣的讀者可查看本站專題:《jQuery切換特效與技巧總結》、《jQuery拖拽特效與技巧總結》、《jQuery擴充技巧總結》、《jQuery常見經典特效匯總》、《jQuery動畫與特效用法總結》、《jquery選取器用法總結》及《jQuery常用外掛程式及用法總結》

希望本文所述對大家jQuery程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.