Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FileUpload.FileName behavior when there is a semi-colon or opening bracket in the file name

I have a FileUpload control, and there are certain restrictions on the file name, certain characters that shouldn't be allowed. The following code works for most characters, but for some reason not others:

if (FileUpload1.HasFile)
{
    if (FileUpload1.FileName.Contains('#') ||
        FileUpload1.FileName.Contains('&') ||
        FileUpload1.FileName.Contains(';') ||
        FileUpload1.FileName.Contains('{') ||
        FileUpload1.FileName.Contains('}') ||
        FileUpload1.FileName.Contains('+'))
    {
        //error: bad character detected
    }
}

I could probably do this another (better) way, with a regular expression, but first I really want to know why the above doesn't work.

The following characters are detected in the FileName:

# & } +

The following characters are not detected in the FileName:

; {

Why?

Edit: examples of file names that I've tried.

  • Final+Version.pdf //+ detected

  • Final;Version.pdf //; NOT detected

  • WhyHello{there.pdf //{ NOT detected

  • Policies}20120303.pdf //} detected

As mentioned in the comments below, there is no problem with these characters on strings, so maybe it's a problem with the value of "FileName" or the way the FileUploader handles the file name?

Edit 2: Breakpoint step through shows that, using Policies{20120303.pdf as an example, the value of FileName is Policies.pdf. So this is not a problem with .Contains() anymore, but with FileUpload and FileName.

So the new question is, how can I handle this? I don't want files with these characters to go through, and I don't want the submitted files to be named differently from what the user named them. So if someone tries to submit 'Policies{20120303.pdf', I need one of two results:

  1. invalid name is detected, procedure aborted
  2. submit the file with the complete and original name, Policies{20120303.pdf

Edit 3: If I submit a file with the following name: foo;bar{baz.txt, the value of FileUpload.FileName is "foo.txt"

Edit 4: Thanks to some helpful comments below, I tried using a different browser (Chrome), and it works just fine! The file name stays intact, even with foo;bar{baz.txt. I use Opera, and it wasn't working. I guess that narrows it down quite a lot to a browser specific issue. I don't think there's gonna be any way to make this work properly in Opera, unless someone has any ideas?

like image 430
CptSupermrkt Avatar asked Sep 10 '12 01:09

CptSupermrkt


People also ask

Can you use semi colon in a file name?

Yes. A semi-colon is a legal character in a Windows file-name.

What does a semicolon mean in a file path?

Specifies the search path for the executable files. Include a colon ( : ) separator between the path names on UNIX systems. (Use the semicolon ( ; ) separator between path names on Windows systems.) You can specify the search path in various ways.

How do you put a colon in a folder name?

The character code for the colon-like symbol is 02F8. The reason for the Subscript setting is to position the symbol lower relative to its vertical position. The bold and larger font settings are applied so that the symbol is more discernible on the page, and have no affect when used in a file or folder name.


1 Answers

I used the following code in my program to check the filename:

filename = txtFileName.Text;
if (filename == "" || (filename.IndexOfAny(System.IO.Path.GetInvalidFileNameChars()) != -1))
{
// Ask for a new file name, or whatever else you need to do.
}

In my case, however, I'm checking the user provided string before setting the file name in the file object. I think that would probably solve your problem as well.

I'm guessing that somewhere there's a function that alters the string before you get to the point of verifying it.

like image 162
PAUL DUFRESNE Avatar answered Oct 12 '22 00:10

PAUL DUFRESNE