views:

545

answers:

3

This is driving me crazy, and I feel like I should know this, but for some reason it's just not coming to me. I scoured the related questions but no dice.

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

Thanks, I'm really not a n00b even though sometimes it may appear that way :\

+1  A: 

(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

hadi teo
+4  A: 

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.

womp
how do i create a filestream without knowing the path of the file i'm trying to get?
Jason
I think you're a bit confused. The uploaded file exists as a stream of bytes as part of the HTTP request. There is no relation between this stream of bytes and a file on the file system, until you save it to disk.
womp
@Jason: As Hadi said, use the FileContent property to use the underlying stream of the uploaded file.
shahkalpesh
Yep... it's all covered in the links that we posted... looks like we both linked the same page.
womp
A: 

Basics of Http uploading

SUMMARY: It's always better, for me, to understand WHY and HOW something is happening. If you say "just because" or "whatever, you just add that, and it works" then I think that's sad. For some reason while many folks understand FORM POSTs and generally how form data is passed up to the server, when a file is transferred many just conclude it's magic.

Why do we have to add enctype="multipart/form=data" on our forms that include file uploads? Because the form will now be POSTed in multiple parts.

File input (or “upload”) in HTML forms

adatapost