By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,793 Members | 1,257 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,793 IT Pros & Developers. It's quick & easy.

JSP - input file, get fileName

100+
P: 144
hi,

i have this form in my .html file

Expand|Select|Wrap|Line Numbers
  1. <form method="post" action="test.jsp">
  2.             <table width="100%">
  3.                 <tr>
  4.                     <td>Attachment File</td>
  5.                     <td><input type="file" name="attachment"></td>
  6.                 </tr>
  7.                 <tr>
  8.                     <td></td>
  9.                     <td><input type="submit" value="Update"><input type="reset"></td>
  10.                 </tr>
  11.             </table>            
  12.             </form>
  13.  
now, i don't want to upload the file chosen to the server, i only want to get the filename of the chosen file by the user.

how can i do that?

thanks in advance
Sep 22 '08 #1
Share this Question
Share on Google+
5 Replies


10K+
P: 13,264
I'll move this to the Javascript forum.
Sep 23 '08 #2

P: 93
Use this code it will give you the file name.But at first give Id to the field i.e where you are taking the input file name

as [HTML]<td><input type="file" id ="attachment" name="attachment"></td>[/HTML]



Expand|Select|Wrap|Line Numbers
  1. function getTheFileName(){alert();
  2.  
  3. var pathname=document.getElementById("attachment").value;
  4. alert(pathname);
  5.  
  6.  
  7.  
  8.  
  9. var partOfPath=pathname.split('\\');
  10.  
  11. var lengthOfPath=partOfPath.length;
  12. var fileName=partOfPath[lengthOfPath-1];
  13. alert(fileName);
  14.  
  15. }
Sep 23 '08 #3

rnd me
Expert 100+
P: 427
does that code work in firefox 3?

there is no path on file input.value in ff3, so the hard-coded -1 makes me skeptical.
Sep 23 '08 #4

Expert 100+
P: 392
does that code work in firefox 3?
If it works at all in any browser I would think it would be a security issue. Most browser security models do not allow JavaScript to access the file input tag to prevent local file system information from being sent back to the server. Returning just the file name and not the path I guess is not going to be a problem.
Sep 23 '08 #5

rnd me
Expert 100+
P: 427
If it works at all in any browser I would think it would be a security issue. Most browser security models do not allow JavaScript to access the file input tag to prevent local file system information from being sent back to the server. Returning just the file name and not the path I guess is not going to be a problem.
actually, firefox 3 gives javascript full binary or text access to data in the file inputs...

all other browsers do not give this access, but they do give the full path.
Sep 24 '08 #6

Post your reply

Sign in to post your reply or Sign up for a free account.