Question

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...

Was it helpful?

Solution

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.

OTHER TIPS

(transferring via byte array through a webservice).

Since you're currently requiring the File Byte Array, why not access the file's Byte Array through theFile.FileBytes property ?

Here is the reference to the FileBytes property of the FileUpload web control : http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.fileupload.filebytes.aspx

If you would like to access the Stream object directly, you can utilize the FileContent property. Here is the reference to the FileContent property of the FileUpload web control : http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.fileupload.filecontent.aspx

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top