Link to home
Start Free TrialLog in
Avatar of shragi
shragiFlag for India

asked on

store the path from browse button

In jsp how do we create browse button.... to locate a folder or file...

and how can we store that location...

becoz here I ask my user to select a place to store their file...

I want to use the same location later on in my code...

so how we get that location...

generally textfields and all can be retrieved by getparameter
but is it possible to get the location
Avatar of rrz
rrz
Flag of United States of America image

Are you talking about files on the server ? If yes, then you can use something like the code below here. It stores the user's section in a session-scoped object.  You can get  the selected  file name  in your code with
String fileName = session.getAttribute("selectedFile");
Are the files all in one folder  ?  Below I assume that folder is in your web app's root folder.
<%@ page import="java.io.File" %>
<%
  String selectedFile = request.getParameter("selectedFile");
  if(selectedFile == null || "Please make a selection".equals(selectedFile))selectedFile = "not selected";
  session.setAttribute("selectedFile", selectedFile);
%>
<html>
<body>
<form>
<select name="selectedFile" >
<option value="Please make a selection" >Please make a selection</option>
<%
   File folder = new File(application.getRealPath("/test"));
   String[] fileList = folder.list();
   //for(int x = 0 ; x < fileList.length ; x++){
   for(String fileName : fileList){
           out.print("<option value=\"" + fileName + "\">" + fileName + "</option>");
   }
%>
</select>
<input type="submit" />
</form>
Selected file is ${selectedFile}
</body>
</html>

Open in new window

>In jsp how do we create browse button.... to locate a folder or file...  
If you are talking about files on the user machine, then to upload a file, you must use multipart/form-data    look at  
http://www.htmlcodetutorial.com/forms/_FORM_ENCTYPE.html   
To parse the upload at server, you can use  software from
http://commons.apache.org/fileupload/
For example code see    
https://www.experts-exchange.com/questions/23504709/How-to-upload-a-text-file-on-a-particular-location-on-oracle-server-using-JSP.html   
I understood that you want to know location on the client side.

Then this is related discussion based on C, not Java, but still
it shows how you can direct the user where to load it on the local machine:

http://www.west-wind.com/weblog/posts/76293.aspx

But it does not say, how to get back the location where user finally chose to put it,
though one guy asked this question explicitly in the discussion.

Another person  mentioned that if that would be possible then it would be a huge security
risk, as you on the server will get some information about the structure
of directories on the client. So maybe it is not possible at all.

Of course if you are in a intra-corporate environment and can reach some
client network locations from your server - that may help you, and that's what I often
do, but that of course it does not solve your question.

Interesting, maybe someone still comes up with some idea.




Avatar of shragi

ASKER

I want to upload file from my computer... not from server...

now after getting the file path I will just parse it...
to fill the fields...

so how to browse that file...

what you mean - from my computer?
Your computer, you mean,  is a client?
But you are designing server side program, as you are talking about jsp?
Please, explain once again where is client where is server, so there is no confusion.
I agree with for_yan.  Please give us more details. Show us whatever code you have already.
Avatar of shragi

ASKER

here is the code i had written to locate a file...

<tr align="center">
<td align="right">Browse a location </td>
<td>
<input type="file" name="file_location" size="chars">
</td>
</tr>


but I need want to give an option to customer to browse a location to save the file....

in the above code the file can be selected... i.e., the path in the above code
is something like below

c://users//file1.txt      i.e., the user selected file1.txt...

but i want to ask user to select a location to save file2.txt

he should be able to select the folder instead of file...

am I clear...
So you are writing code on the server.
Then your user operates on the client using your Web application, say JSP using her local browser.
You offer to user to go to certain location, but she may choose using the browsing component
save this file to any location on her local machine which she chooses.
You want that your server side program should get information on the filepath which she chose
to store this file on her local system
Is this my understanding correct?
Avatar of shragi

ASKER

not exactly...

I am not offering my client any location...
I am asking my client to choose a place to store a file... on her local machine...

actually the problem...is
I am asking my client to fill a form... then at the end I am asking her to choose a location to save the file

the file contains... the details that client has filled in the form...

when she chooses the location... and click submit... button i do some modifications to the data that client entered in the form and save in a file and store in the location where client choosed...

I did all the form work and server side thing...i am struck with this browsing file...

>I am asking my client to choose a place to store a file... on her local machine...  
In that case, JSP(my expertise) can't help you.  If you want to do that with Java, then maybe you could use a signed applet.  
I agree with rrz@871311:  from server side through the browser the only way you can
access local file system is through signed applet. And even the filepath on the local system
should not be communicated to the server directlyfrom the browser - otherwise it would be breach of security.

You can only ask the use herself to paste the local path into the textbox in your form
(she may first browse there independently and cut and paste the path into the text window
of your form). Still I'm not sure what use could you make of her local path when you
get it back to your server side program - you'll still not be allowed to access it through browser.

With signed applet it is another story - it will operate as Java application running on
her computer and you can send all the paths to yourself from the Applet.
ASKER CERTIFIED SOLUTION
Avatar of gordon_vt02
gordon_vt02

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gordon_vt02
gordon_vt02

Once you've set the headers, just write the bytes for the file you created to the response.