Below is what I have in jsf 2.0
<tr>
<th width="19%" align="left"><h:outputLabel value="Photo" /></th>
<th width="2%">:</th>
<td width="19%"><h:inputText value="#{PersonalInformationDataBean.photo}" size="80" id="photo" readonly="true"/>
<input type="file" name="pathPhoto" onchange="alert('you changed me...'+window.document.patentForm.pathPhoto.value);window.document.patentForm.photo.value=window.document.patentForm.pathPhoto.value"/></td>
</tr>
When I click on button Choose File and select some file, I get different path.
Chrome & Safari
I get path name as C:\fakepath\jobs_dubizzle.rtf. I always get output as C:\fakepath\FileName irrespective of any location.
Mozilla Firefox
It gives me full path perfectly.
Any idea why I am getting like this? (Different behavior across browsers.) And how to overcome? I need full path of that file so that I can use same path for storing the path in MySQL data.
Update 1
What I wanted to do is when I submit the form, I want that file to get uploaded in mysql data. Right now I would be selecting images. To upload images, I would be using code as provided here.
As you see, I would be needing full path for statement below.
File image = new File("C:/image.jpg");
That is why, I was curious to get full path of the selected file.
Any idea to get this?
In a web application, you wouldn’t want to rely on locating a file on the server. The best you can do is store at the root of application and retrieve like this:
But Ideally if you are using JSF and have Servlet 3.0 container, I suggest you to use File Upload written by BalusC in his blog which can be found here.
If you do not have Servlet 3.0, then you can use the File Upload example written in Core JavaServer Faces textbook. You can download the source code from the author’s site it is located in ch13 directory.
And you can do whatever you wish to do like saving to database in the action method when you submit the form using any of the above components.
Alternatively, if you can use libraries like PrimeFaces, they have a nice file upload component which you can use out of the box.