Because the file control is automatically emptied when it fails to upload, the text box is used to save the upload path, and the style of the control needs to be compatible under different browsers. Here are examples of your own use
//Initialize to determine browser version, use different styles depending on version
functionGetExplorer () {//IE if(Navigator.userAgent.indexOf ("MSIE") >=0) {document.getElementById ("1"). Style.display = "Block"; document.getElementById ("2"). Style.display = "None"; document.getElementById ("FileUpload"). ClassName = "File"; document.getElementById ("box"). ClassName = "File-box"; } //Firefox Else if(Navigator.userAgent.indexOf ("Firefox") >=0) {document.getElementById ("1"). Style.display = "None"; document.getElementById ("2"). Style.display = "Block"; document.forms[0].elements["PropertyName"].style.width= "625px"; } //Chrome Else if(Navigator.userAgent.indexOf ("Chrome") >=0) {document.getElementById ("1"). Style.display = "Block"; document.getElementById ("2"). Style.display = "None"; document.forms[0].elements["PropertyName"].style.width= "495px"; document.getElementById ("Iebtn"). Style.display = "None"; document.getElementById ("FileUpload"). Style.width = "93px"; } //Safari Else if(Navigator.userAgent.indexOf ("Safari") >=0) {document.getElementById ("1"). Style.display = "Block"; document.getElementById ("2"). Style.display = "None"; document.forms[0].elements["PropertyName"].style.width= "585px"; document.getElementById ("Iebtn"). Style.display = "None"; document.getElementById ("FileUpload"). Style.width = "90px"; }}
Code of the style
. File-box{position:relative;width:55px}. BTN{width:55px;}. File{position:Absolute;Top:0; Right:0px;Height:24px;Filter:Alpha (opacity:0);Opacity:0;width:0px}
Code for HTML
Because of the use of struts2
The name of the control is Forminfo.propertyname
When modifying an occasion, the value of the control is preceded by the action set by Request.setattribute ("Form", Pform), and the JSP is received with the following write
<TableAlign= "Center"><TR><TD><S:textfieldname= "Forminfo.propertyname"value= "%{#request. Form.propertyname} "size= "+"maxlength= "+"style= "width:620px;"ReadOnly= "true" /></TD><TDID= "1"><DivID= "box"> <inputtype= "button"value= "Reference"class= "BTN"ID= "Iebtn" /> <inputtype= "File"name= "Filepropertyname"ID= "FileUpload"size= "1"onchange= "Change ()" /></Div></TD><TDID= "2"><inputtype= "button"name= "Upload"class= "BTN"ID= "Upload"value= "Reference"onclick= "selectpdf ()"/></TD><TD><inputtype= "button"value= "クリア"class= "BTN"onclick= "delpdf ()"/></TD></TR></Table>
Click the button to display the path to JS in the text box
//assign a path to a text box when the value of file changesfunctionChange () {//IE if(Navigator.userAgent.indexOf ("MSIE")!=-1| | Navigator.userAgent.indexOf ("Chrome")!=-1| | Navigator.userAgent.indexOf ("Safari")!=-1) { varLocalPath = document.getElementById ("FileUpload"). Value; document.forms[0].elements["propertyname"].value =LocalPath; } //FF Else if(Navigator.userAgent.indexOf ("Firefox")!=-1) {Readfilefirefox ()}}//FFfunctionReadfilefirefox () {Try{Netscape.security.PrivilegeManager.enablePrivilege ("Universalxpconnect"); } Catch(e) {return; } varFilename=document.getelementbyid ("FileUpload"). Value; varFile = components.classes["@mozilla. Org/file/local;1"]. CreateInstance (Components.interfaces.nsILocalFile); Try{File.initwithpath (Filename.replace (/\//g, "\\\\") ); } Catch(e) {if(E.result!=components.results.ns_error_file_unrecognized_path)Throwe; return; } if(file.exists () = =false) {alert ("File" + fileName + "' not found."); return; } document.forms[0].elements["propertyname"].value =File.path;
}
//click the button under Firefox to open the file control pop-up Files Check dialog box
functionselectpdf ()
{
document.forms[0]. Fileupload.click ();
}
Struts2 JS Get the absolute path of uploading files, compatible with IE and FF