Link to home
Start Free TrialLog in
Avatar of ASPNet_Developer
ASPNet_Developer

asked on

How to get path of the file from FileUpload control in ASP.Net?

I want to get the complete path of the file selected in FileUpload ASP.Net control.
I am currently using fileUpload1.PostedFile.FileName which is returning the compltete path for IE browser, but it is not working for firefox.
Can someone suggest how can I get complete path for all the browsers.
ASKER CERTIFIED SOLUTION
Avatar of MaxOvrdrv2
MaxOvrdrv2

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of abhinayp
abhinayp

Aslo, Are u using the fileupload with an updatepanel?
If yes, fileupload control doesnt work with Async Postbacks.

http://forums.asp.net/p/1105208/1689084.aspx
how silly of me... try this:

Server.MapPath(FileUpload1.FileName)

that should get it working in FF.
Avatar of ASPNet_Developer

ASKER

MaxOvrdrv2: no luck, it is not returning the path.
which one... server.mappath or the original posting? i thought you just wanted to open it in FF... and FF doesn't automaticaly put @file:///@ when it sees a drive whereas IE does... but then i re-read your question and realized what you wanted to do... the MapPath one should work, i have a working version here.
i have already tried Server.MapPath(FileUpload1.FileName), it is not returning the correct path
Both of them
hmmm... works fine for me... what OS are you on, and what is the path that you're expecting?
oh wait yeah i see what you mean... hold on...
yeah... short answer... you can't...

basically, you have to understand that a file upload object should only be passing the filename, to give you some clues as to the file itself (using the extension)... but getting the full file path is not necessary as it is supposed to be getiing uploaded to the server. Basically, none of the recent borwsers will give out the full path (security risk), except for IE, most likely because it's tied into the OS.

The other option would be to build a VB.Net app that would allow users to browse folders and files, then returns the path chosen, instead of using the FileUpload object. But now we're talking web application instead of website so it's up to you to see if you want that or not now.
Avatar of gery128
That is not possible in newer browsers.

In all the newer browsers FF, Chrome, IE8 and above this feature has been disabled due to security reasons. Why you need path of a file stored in client's systems?
I am converting the image file to bytes
then use the stream object of the postedfile... you don't need to have the path for that.
I have to pass the whole path to convert the file to byte array
Below is my code
Dim _fileInfo As New IO.FileInfo(fileUploadJurisSealImg.PostedFile.FileName)
Dim _NumBytes As Long = _fileInfo.Length
Dim _FStream As New IO.FileStream((fileUploadJurisSealImg).ToString.Trim, IO.FileMode.Open, IO.FileAccess.Read)
Dim _BinaryReader As New IO.BinaryReader(_FStream)
_tempByte = _BinaryReader.ReadBytes(Convert.ToInt32(_NumBytes))
_fileInfo = Nothing
_NumBytes = 0
_FStream.Close()
_FStream.Dispose()
_BinaryReader.Close()

----------------------
How can I pass the path?
you see, this would never work, because you're executing this code on the server (back-end), so this wouldn't work from the client anyway... so my advice to you would be to use a specific folder on your server to store the uploaded file, and then when you call your function, just pass it the newly uploaded filename, and use the folder you already know.

example (pseudo-add/fix to your environment):
Button1.Click Sub
FileUpload1.SaveAs("c:\inetpub\wwwroot\WebSite1\Files\Images\" & FileUpload1.FileName)
Dim _fileInfo As New IO.FileInfo("c:\inetpub\wwwroot\WebSite1\Files\Images\" & FileUpload1.FileName)

and go from there.



The postedfile has not been saved on the server so there is no path. You can either save the file first or directly get a stream from it

http://msdn.microsoft.com/en-us/library/system.web.httppostedfile.inputstream.aspx
@ASPNet_Developer,

If you want the byte array of uploaded file than you can simply use following:


fileUploadJurisSealImg.FileBytes

This will return Byte array, no need to save and then read file through file stream.

Hope it helps.

I tried but it is not wroking, how do I mplement this?
_bytes=fileUploadJurisSealImg.FileBytes
ya, correct.
_bytes = fileUploadJurisSealImg.FileBytes

this should work if _bytes is Byte Array variable.

What error message you get? or what values you get in byte array variable ? or is it null ?
Dim _byte As [Byte]() = fileUpload1.FileBytes
It is throwing error that 'FILE MEMBER IS NOT A MEMBER OF STRING'
I am getting file bytes in code-behind of vb.net page like this:

 Dim _bytes As Byte() = FileUpload1.FileBytes

You should get any error by using above line. Where are you writing the above line?
You should NOT get any error by using above line. Where are you writing the above line?
Thanks.