The example in this article describes the method of uploading images and files using Struts2+jquery.form.js. Share it with everyone for your reference, the details are as follows:
jquery.form.js is an official jQuery plug-in that supports asynchronous file upload. Official website: http://plugins.jquery.com/form/
Combined with Struts2 to easily upload files in three steps
Generally, a page may have more than one Form form, so submitting a form on one side will affect another form. For this reason, the image upload form can be uploaded using the non-refresh submission method, which is an asynchronous upload. At this time, jquery .from.js comes in handy.
1. HTML
Import this jS to the page and write the upload form
<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>
2. 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(); } });
3. 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 **/ ...... }
4. Configure Struts2.xml
<!-- 图片的异步上传 --> <action name="uploadPic" class="com.wise.hrm.action.notice.uploadPicAjax"> </action>
Okay, I’ve finished writing from the page to the backend. Then you can upload it. complete!
Readers who are interested in more jQuery related content can check out the special topics on this site: "JQuery switching effects and techniques summary", "jQuery drag effects and techniques summary", "JQuery extension skills summary", "jQuery common classic special effects summary", "jQuery animation and special effects usage summary", "jquery selector usage Summary " and "Summary of jQuery common plug-ins and usage "
I hope this article will be helpful to everyone in jQuery programming.