Elegant way to get name of uploaded file?

G

Guest

Hello,

When it's needed to upload a file, and get the name of it:

uploadfile is a HtmlInputFile type:

string NameOfFile = uploadfile.PostedFile.FileName;

NameOfFile will not only get the name of the file, but the complete path on
which it was on the client ("C:\somefolder\test.exe"). How to get just the
file name (test.exe), without path information?

I know, I could just cut the string, search for the appearance of "/" or "\"
cut the rest etc. but, isn't there a more elegant way?
 
S

Siva M

System.IO.Path.GetFileName()

"the friendly display name"
Hello,

When it's needed to upload a file, and get the name of it:

uploadfile is a HtmlInputFile type:

string NameOfFile = uploadfile.PostedFile.FileName;

NameOfFile will not only get the name of the file, but the complete path on
which it was on the client ("C:\somefolder\test.exe"). How to get just the
file name (test.exe), without path information?

I know, I could just cut the string, search for the appearance of "/" or "\"
cut the rest etc. but, isn't there a more elegant way?
 
P

Peter Rilling

This brings up an interesting issue. What is the path when other platforms
such as Mac or Unix upload a file to the server? Each platform has their
own way of representing paths and will the Path class be able to take this
into account?
 
G

Guest

Hi Peter,

The MSDN documentation lists only Windows products under the "Platforms" for
this class. So I do not really know how it will work when receiving files
from the Unix or Mac. I have not tried it myself.

If any one has Unix or Mac, wants to try it and let us know the result, then
they can try this page from my website:
http://www.webswapp.com/demos/PathClass.aspx which will display back the
outcome; whether the Path method catches an exception or manages to get the
filenames correctly.
 
G

Guest

Tried it from Linux, it works.

Btw. the description from the MSDN page says it already, that it is
cross-plattform:

"Performs operations on String instances that contain file or directory path
information. These operations are performed in a cross-platform manner."
 
P

Peter Rilling

Didn't mean that the Path class would be invoked on a Mac computer, but if
you are running a website on Windows, and a Mac client (using something like
the Safari browser) visits your webpage and uploads a file using the file
upload tag, then the path received by the server would probably be the path
would probably be Mac formatted.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top