-1

I want to get a browsed files path. i tried like it

Object objUploadEvent = ctx.getTriggerEvent();
        if (objUploadEvent != null && (objUploadEvent instanceof UploadEvent)) {
            upEvent = (UploadEvent) objUploadEvent;
        }
        if (upEvent != null) {
            Media media = upEvent.getMedia();
            File file=new File(media.getName());
            this.path = file.getAbsolutePath();
        }

But getAbsolutePath is giving path from eclipse. say if my file is in c://doc/abc then it should give path as c://doc/abc/myfile.txt

Thanks

Romi
  • 4,833
  • 28
  • 81
  • 113

3 Answers3

0

If the file is on the client (where the browser is), this is probably not possible with the usual file uploader. This question has some alternatives that solve the exact same problem that you are facing

If the file is on your server, try file.getCanonicalPath(). This should give you the entire path.

Also see: What's the difference between getPath(), getAbsolutePath(), and getCanonicalPath() in Java?

By the way, you can get the directory from which your program was started, the eclipse directory, using System.getProperty("user.dir")). However I don't recommend using this if all you need is the path to a file.

Community
  • 1
  • 1
Navin
  • 3,681
  • 3
  • 28
  • 52
  • No, both canonical nad absolute giving the same C:\softwares\eclipse3.5\eclipse3.5\AMANDA_docx – Romi Jan 15 '13 at 06:21
  • I thought you wanted the path that starts at C:\ . Are you looking for the relative path? – Navin Jan 15 '13 at 06:25
  • @Romi By the way, are you sure that that file exists on your file system? I am asking because you created it using only its name. – Navin Jan 15 '13 at 06:27
  • i want a path like if its in d:\ then it should give from d only not starting from my current working directory – Romi Jan 15 '13 at 06:27
  • @Romi Is the file located on your server? – Navin Jan 15 '13 at 06:29
  • No, i want to browse from my system – Romi Jan 15 '13 at 06:31
  • @Romi Oh I see. As the other answer said, this is probably not possible with the usual file uploader. [This question](http://stackoverflow.com/questions/408735/javascript-file-uploads) has some alternatives that solve the exact same problem that you are facing – Navin Jan 15 '13 at 06:36
0

If you print out the media.getName() to your log, you will most probably see that it is just the file name, without path: myfile.txt

That's because it is up to the browser how much information about a file they send along with the uploaded bytes. And most browsers just give you the name without path. Try opera, there you should get the complete filepath.

rompetroll
  • 4,781
  • 2
  • 37
  • 50
  • yes, you are right. am getting only myfile.txt in media.getName(). but i want to get path in all the browsers not only in opera – Romi Jan 15 '13 at 06:26
  • well, you won'ẗ get it :) unless you send it in in a separate form field – rompetroll Jan 15 '13 at 08:24
0

If I understand correctly, you want the location of the source file on the client as opposed to the location of the file on the server?

This is not possible for security reasons, sorry.

You'll find plenty of discussion about this on StackOverflow and elsewhere (sometimes in reference to the 'fakepath' symptom of some browser's implementation of this security feature.

Without going into too much detail, the reasoning for this security feature is quite simple; no website should need to (read: be able to) find out anything about a user's file system. As such, when a file is uploaded to a server only the file data should be sent.

Technically, it was possible in some older browsers (eg: IE6 I believe) but is a pretty ubiquitous security feature at this point and you would be ill fated to depend on this information.

Community
  • 1
  • 1
Sean Connolly
  • 5,692
  • 7
  • 37
  • 74