Jason Jason -4 years ago 88
ASP.NET (C#) Question

Get the absolute path of a file to be uploaded

I am uploading a file using a simple FileUpload control named

theFile
(ASP.NET). I'm trying to get the absolute path of the file, but
thefile.PostedFile.FileName
and
thefile.FileName
are the exact same, just the file name, no path! I can't use Server.MapPath because I will be saving this file on a different server (transferring via byte array through a webservice).

It breaks at this line:

Dim fStream As New FileStream(thefile.FileName, FileMode.Open, FileAccess.Read)


because it is taking the filename and mapping it to the relative path of my VS! I need the absolute path...

Answer Source

A file uploaded through HTTP will never contain the full path on the remote (client) machine - it could give away information about their directory structure, and so is considered a security risk. Plus, what use would it be? If someone is uploading you a file from over the internet, why would you be trying to open a filestream on your local (asp.net server) machine to the path that existed on their machine?

Uploaded files actually come through as a stream of bytes as part of the request. You need to access the FileBytes property of the control to get the file, or call the SaveAs() method to save it to the server. In your case, you could probably just get the bytes and send them off to the webservice call you need to make.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download