473,394 Members | 1,889 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

JSP - input file, get fileName

144 100+
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
5 13871
r035198x
13,262 8TB
I'll move this to the Javascript forum.
Sep 23 '08 #2
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
427 Expert 256MB
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
pronerd
392 Expert 256MB
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
427 Expert 256MB
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

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

Similar topics

2
by: Xam | last post by:
Hello everybody Do you know of a javascript routine that can warn if there are any pre-defined invalid chars in the filename of an INPUT file box before it is submitted with the submit button. ...
10
by: Brian Henry | last post by:
Hi, I am having a problem with an attachment system I made... it works with files up to ~3MB in size then after that if you try to upload a file it just goes to a "Page can not be displayed" page...
10
by: darrel | last post by:
I have an input type="file" field that I am using to accept a file upload. This works, but I'm having problems with the filename property. In firefox, this: MyInputField.postedfile.filename ...
9
by: sherifffruitfly | last post by:
Hi, I've a got a little (exercise) program that reads data from a file and puts it into struct members. I run into trouble when one of the data pieces is comprised of several words (eg "john...
3
by: Ron Hinds | last post by:
I have the following code on my ASP page: <INPUT type=file name="FileUpload" value="<%=FileName%>"> I'm passing the value of FileName as part of the Query String and assigning it like so: ...
5
by: hrpreet | last post by:
Hi All, I need the file chooser in the jsp, just for brosing and saving the file path in the database, so i have used the following code.I dont need to read the file content. I have to make it...
15
by: Larry Bud | last post by:
I rarely crosspost, but this affects both ASP and Javascript REALLY odd bug that I ran across in ASP 3.0. I have an input type of file, user clicks browse, then places his cursor in the...
1
by: Iaintnofool | last post by:
Hi I'm stuck on a project using VB 2005 express edition. I'm having trouble reading in data into my code from a Text File. I'm using an openFileDialog so the user can select a file to open. The...
3
by: John Williams | last post by:
I'm writing a stagenography program to experiment with how it works. The algorithm I'm using appears to be producing the correct result...however I'm struggling with the file input. I never...
7
by: Ed Jay | last post by:
I'm trying to validate a form's file input elements using javascript, but I'm getting nowhere. How can javascript test if a file has been selected using a file form input element? -- Ed Jay...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.