<%@page language="java" contentType="text/html;charset=utf-8" pageEncoding="utf-8"%> <%@taglib prefix="s" uri="/struts-tags"%> <! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" > <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Struts 2 File Upload</title> <mce:script language="javascript"><!-- function uploadform_Validator(myuploadform){ if(document.all.myFile.value=="")//檢查是否提交了上傳檔案 否彈出提示 不需要這一功能的刪除即可 { alert("沒有選擇上傳的檔案。"); myuploadform.myFile.focus(); return false; } var last=myuploadform.myFile.value.match(/^(.*)(/.)(.{1,8})$/)[3];//截取提交上傳檔案的副檔名 last=last.toUpperCase(); //設定答應上傳檔案的副檔名 if(last=="DOC") { //這裡可以添加顯示格式正確後的代碼 } else { alert("只能上傳 .DOC 檔案,請重新選擇需要上傳的檔案。"); return false; } return true; } // --></mce:script> </head> <body> <div> <s:form name="fileUpload" action="fileUpload" method="POST" enctype="multipart/form-data" onsubmit="return uploadform_Validator(this)"> <s:file name="myFile" label="Word File" /> <s:textfield name="caption" label="Caption" /> <s:submit /> </s:form> </div> </body> </html>
轉自http://www.itzhe.cn/news/20080311/106898.html並加以整理。