Last listened to The Getaway by Immortal Technique (on 10 Mar 2010, 14:39)

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.

Hello world

My first post will be a quick one just to state what the purpose and aims of this blog (and website in general) are.

I go through websites like there's no tomorrow - it's a job and a hobby. However, I intend for this one to stick around and so I'll outline what the aims and purpose of this site and blog are. So, I'm going to:

  1. Post and discuss code (C#, .NET, HTML, CSS, whatever) when I think something might be helpful to other people.
  2. Try out new stuff like playing with some web 2.0 sites and services.
  3. Practice and keep up-to-speed with coding, web design, etc.

The purpose of this blog/website will not be for:

  1. Posting personal and useless stuff about my life, etc. I'll keep everything tech-related.

I think that just about covers it.