aspose file tools*
The moose likes Struts and the fly likes How can i restrict the selecting file type to XML and TXT in file upload.. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "How can i restrict the selecting file type to XML and TXT in file upload.." Watch "How can i restrict the selecting file type to XML and TXT in file upload.." New topic
Author

How can i restrict the selecting file type to XML and TXT in file upload..

Sudheesh
Greenhorn

Joined: Sep 22, 2005
Posts: 11
Hi,

I am using Struts Upload Bean. I am also using html:file tag in the JSP. Now when i click on the browse button, A file dialogue window appears. The problem id the i want to show only text and xml files in the dialog box. Now the files of type combo box in the dialogue box is showing *.*, *.html, *.pictures (jpg/gif) options. In place of this i need to show only txt and xml files. How can i restrict the file type to XML and TXT

Regards,
Sudheesh...
David Ulicny
Ranch Hand

Joined: Aug 04, 2004
Posts: 724
I think, there is unfortunately no such posibility.


SCJP<br />SCWCD <br />ICSD(286)<br />MCP 70-216
SuprajaY Yasoda
Greenhorn

Joined: Oct 17, 2005
Posts: 17
You can try javscript validation some what like this

function checkFileExtension() {
//get the file extension in a variable
var extension = document.FBITT.ffAttachment.value;
var isValid = false;
var strExtensions="";
//alert("ext before while:"+ extension);
//substring
while(extension.indexOf('.') != -1) {
//alert("gkmkc");
//alert(extension.length);
//alert(extension.indexOf('.'));
extension = extension.substring((extension.indexOf('.')+1), extension.length);
//alert("ext :"+ extension);
//alert("ext :"+ extension);
}
//alert("ext after while:"+ extension);

//iterate and valiadte against the arraylist in the formbean
<%
//i hate to do this, but alas.. may i think of a better way given the time...
sg.gov.sco.gebiz.webitt.formbean.itts.FBITT fbFBITT = (sg.gov.sco.gebiz.webitt.formbean.itts.FBITT) request.getSession(false).getAttribute("FBITT");
for(int i=0; i<fbFBITT.getArrValidFileExtensions().size(); i++) {
%>
var validExtensionU = '<%=((String)fbFBITT.getArrValidFileExtensions().get(i)).toUpperCase()%>';
var validExtensionL = '<%=((String)fbFBITT.getArrValidFileExtensions().get(i)).toLowerCase()%>';
strExtensions = strExtensions + validExtensionL + ", ";
//make this case independent in java script
//alert(validExtension)
if(extension==validExtensionU || extension == validExtensionL) {
//set isValid to true, though redundant
isValid = true;
}
<% } %>

if(isValid==false) {
alert("Please select a file with one of the following file extension:\n"+strExtensions.substring(0, strExtensions.length-2));
}
//alert("isValid :"+isValid);
return isValid;
} //checkFileExtension-ends
Sudheesh
Greenhorn

Joined: Sep 22, 2005
Posts: 11
Sirs,
Thank you very much for your replies. Infact I am planning to call a validation function to check the file extension using Java Script. Anyway thank you very mush for your suggestions...

Regards

Sudheesh K S
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How can i restrict the selecting file type to XML and TXT in file upload..