39

When perform uploading from IE browser, that my backend(org.apache.commons.fileupload) gets full file path.

For other non-IE browsers it gets filename and it's OK because of security.

How to get filename only from input for IE?

Is it possible to do on UI, because I think it's not very graceful to edit apache lib.

Maybe , some parameter exists for input field?

I can do it on server, but interested in UI approach.

royhowie
  • 11,075
  • 14
  • 50
  • 67
sergionni
  • 13,290
  • 42
  • 132
  • 189

7 Answers7

53

There is also an IE option under:

  • Internet Options
  • Security tab
  • In "Internet" or "Intranet" click on Custom Level
  • In the Security Settings scroll down until you see “Include local directory path when uploading files to a server” and disable it.
  • Click OK on Internet Options window and refresh.
Almalerik
  • 1,002
  • 1
  • 11
  • 26
  • 2
    Somewhat official reference: https://msdn.microsoft.com/en-us/library/ms535128%28v=vs.85%29.aspx – dbreaux Dec 15 '15 at 22:18
38

Try this at the server side:

Path.GetFileName(file.FileName)
zb226
  • 9,586
  • 6
  • 49
  • 79
Designworxz
  • 535
  • 4
  • 6
27

The point of the file input is to provide a file. Names that come with it are "whatever the browser vendor feels like using", they aren't guaranteed to have anything to do with the file name on the file system at all.

You can't change what the browser sends.

If you are going to make use of the name sent by the browser, then you need to make sure it is valid for whatever you are going to do with it (e.g. make sure it only includes characters that are allowed in filenames on your filesystem). This makes it something that must be handled on the server (just like any other client supplied data).

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • 5
    ok,thanks, seems that server solution is accepted only, i just read apache advises:http://commons.apache.org/fileupload/faq.html#whole-path-from-IE – sergionni Feb 04 '10 at 13:59
27

There is actually a pretty easy workaround if you are uploading with FormData via Xhr. The FormData.append api allows for you to pass in the filename as the 3rd argument.

const formData = new FormData();
//explicitly setting the file name works in IE 11
formData.append('file', file, file.name);
pllee
  • 3,909
  • 2
  • 30
  • 33
  • 1
    This is the simplest example. As far as the concern of front end being not able to send the correct name, this should be done at front end level instead of working on back end. Besides, I also wanted to show name in a files list so this was the perfect choice. – Imran Faruqi Mar 04 '20 at 04:04
3

You are interested in a UI approach.
You could add a hidden input-field (or JSON entry) that contains just the bare file-name, populated by javascript.

Using the following cross-browser(including IE)/platform javascript function (taken from my answer here, with full explanation and reference), which gets the filename only:

function extractFilename(s){ 
  // returns string containing everything from the end of the string 
  //   that is not a back/forward slash or an empty string on error
  //   so one can check if return_value===''
  return (typeof s==='string' && (s=s.match(/[^\\\/]+$/)) && s[0]) || '';
} 
<input type="file" onchange="alert(extractFilename(this.value));">
Community
  • 1
  • 1
GitaarLAB
  • 14,536
  • 11
  • 60
  • 80
1

Seems like easy to handle this from server side but based on the requirements.

I have tested this in production env and works fine.

String fileName = file.getName();
if (fileName != null) {
    fileName = FilenameUtils.getName(fileName);
}

IE by defaults gives the full path along with the file name, and it causes issue while uploading a file from some shared directory. adding above snippet will resolve the issue and works well in all cases.

Sachchidanand Singh
  • 1,456
  • 1
  • 15
  • 15
-1

if you post by xhr then you can simply provide the file name

post look like this

Rana
  • 1,170
  • 3
  • 14
  • 28
  • 1
    Please **[edit]** your post and show the actual code as text instead of screenshots. Others can't copy and paste from your images. [See here](https://meta.stackoverflow.com/a/285557/1402846) for details. Thank you. – Pang Aug 20 '19 at 07:04