0

I am developing a web application in which for User Registration the user has to select a profile picture and then click on upload button.

So, my question is how to get FileName which user has selected and display.

I am using Struts 2 and JSP.

Roman C
  • 49,761
  • 33
  • 66
  • 176
Coder
  • 116
  • 1
  • 15

2 Answers2

0

if The input tag you are using in your html is

<input type="file" name="MyFile" id="MyFile" />

then you can retrieve your file name in javascript by using the following code

var fileName = document.getElementById('MyFile').value;

But for some security reasons, browsers won't allow you retrieve full filepath of your file.

But I heard old versions of Internet Explorer gives you file path.

Good Luck!

chaitanya89
  • 827
  • 3
  • 20
  • 26
  • Yes I was planning to use this approach ,but for the same reason I am not using it.I may use Struts default facility. Thanks for your concern. – Coder Nov 29 '13 at 06:29
0

If you use the property uploadFileName along with upload property to the action that accepts uploading a file, i.e. the fileUpload interceptor is applied to the action configuration then it will be populated. There's also uploadContentType attribute that could be populated.

private File upload;
private String uploadContentType;
private String uploadFileName;
//getters ans setters assumed

in the JSP use the name attribute to map it to the action

<s:file name="upload"/>
Roman C
  • 49,761
  • 33
  • 66
  • 176
  • Hey I am using your appraoch bt I am getting nullPointerException for following statement- String filePath=servletRequest.getSession().getServletContext().getRealPath("/"); Here getSession is giving null value. I checked all code but where session is going null cant recognize...! – Coder Nov 29 '13 at 12:23
  • Try `ServletActionContext.getServletContext()`. – Roman C Nov 29 '13 at 12:42
  • Hello, I have another doubt. Actually the statement which given i.e. getSession is still giving null value.So I decided not to waste time. So I am now uploading image in D drive and its working great ,but while displaying that image in jsp,it is trying to retrieve it from context path where image does not exist. So my question is how i should change my code to retrieve that image from D:\\ . Is there any solution? – Coder Dec 01 '13 at 06:14
  • Read [this](http://stackoverflow.com/a/20176879/573032) answer and follow the links. – Roman C Dec 01 '13 at 09:41