[Mono-list] File upload in asp.net and Path.GetFileName

Robert Jordan robertj at gmx.net
Wed Oct 31 15:36:09 EDT 2007


David Temes wrote:
> In windows file upload is working fine, but in linux whenever I send
> a file with internet explorer it seems that the FileName property of
> the uploaded file contains "\" that Path.GetFileName does not
> interpret as path separator.

HttpPostedFile.FileName will contain whatever the client (browser)
sent. It could be junk or a Mac OS 8 path or even "/etc/passwd".

> So if I try to upload from my windows machine using IE the file
> c:\test.jpg to later save it in /tmp I end up with c:\test.jpg being
> the file name returned by Path.GetFileName (instead of just test.jpg)
> and trying to save a file as /tmp/c:\test.jpg, and that throws an
> exception.

You have to parse the file name yourself.

> 
> Firefox for instance send only test.jpg as file name and it works
> fine.
> 
> Now my questions:
> 
> 1- is there a best way to solve this problem? 2- if no, is turning
> every \ into / before using Path.GetFileName a good strategy or is it
> a bad practice? 3- Why does IE send all that path information?

Split on the rightmost / or \ and remove all Path.InvalidPathChars
or Path.GetInvalidFileNameChars () from the string.

Robert



More information about the Mono-list mailing list