When using an ASP.NET File Upload control (or the HTML equivalent) it's worth remembering that when you come to use the FileName property in your code, it annoyingly has different values depending on what browser you are using.
If you have one of these on your page...
<asp:FileUpload id="FileUpload1" runat="server" />
... or the HTML version...
<input id="FileUpload2" runat="server" type="file" />
... when you come to get the name of the user-selected file in your code (which for example was a file picture.jpg in C:\Pics\), you will get the following results depending on the users browser:
| Control: |
FileUpload1 |
FileUpload1 |
FileUpload2 |
FileUpload2 |
| Property: |
FileName |
PostedFile.FileName |
Value |
PostedFile.FileName |
| Firefox 2 |
picture.jpg |
picture.jpg |
picture.jpg |
picture.jpg |
| Firefox 3 |
picture.jpg |
picture.jpg |
picture.jpg |
picture.jpg |
| IE7 |
picture.jpg |
C:\Pics\picture.jpg |
C:\Pics\picture.jpg |
C:\Pics\picture.jpg |
| Opera 9 |
picture.jpg |
picture.jpg |
picture.jpg |
picture.jpg |
| Safari 3 |
picture.jpg |
picture.jpg |
picture.jpg |
picture.jpg |
As you can see, it is Internet Explorer that is the odd one out. What this means is that you can't rely on using the FileName property without first checking the value and trimming out the unwanted file path. For example, to get picture.jpg no matter what browser is used, you'll have to do something like this:
string file = FileUpload2.PostedFile.FileName;
int slash = file.LastIndexOf("\\");
if (slash > 0) file = file.Substring(slash, file.Length - 1);
Don't let this catch you out.