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.
ASPNet_DeveloperAsked:
Who is Participating?
 
MaxOvrdrv2Commented:
"file:///" & fileUpload1.PostedFile.FileName
0
 
abhinaypCommented:
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
0
 
MaxOvrdrv2Commented:
how silly of me... try this:

Server.MapPath(FileUpload1.FileName)

that should get it working in FF.
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
ASPNet_DeveloperAuthor Commented:
MaxOvrdrv2: no luck, it is not returning the path.
0
 
MaxOvrdrv2Commented:
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.
0
 
ASPNet_DeveloperAuthor Commented:
i have already tried Server.MapPath(FileUpload1.FileName), it is not returning the correct path
0
 
ASPNet_DeveloperAuthor Commented:
Both of them
0
 
MaxOvrdrv2Commented:
hmmm... works fine for me... what OS are you on, and what is the path that you're expecting?
0
 
MaxOvrdrv2Commented:
oh wait yeah i see what you mean... hold on...
0
 
MaxOvrdrv2Commented:
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.
0
 
gery128Commented:
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?
0
 
ASPNet_DeveloperAuthor Commented:
I am converting the image file to bytes
0
 
MaxOvrdrv2Commented:
then use the stream object of the postedfile... you don't need to have the path for that.
0
 
ASPNet_DeveloperAuthor Commented:
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?
0
 
MaxOvrdrv2Commented:
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.



0
 
CodeCruiserCommented:
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
0
 
gery128Commented:
@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.

0
 
ASPNet_DeveloperAuthor Commented:
I tried but it is not wroking, how do I mplement this?
_bytes=fileUploadJurisSealImg.FileBytes
0
 
gery128Commented:
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 ?
0
 
gery128Commented:
Dim _byte As [Byte]() = fileUpload1.FileBytes
0
 
ASPNet_DeveloperAuthor Commented:
It is throwing error that 'FILE MEMBER IS NOT A MEMBER OF STRING'
0
 
gery128Commented:
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?
0
 
gery128Commented:
You should NOT get any error by using above line. Where are you writing the above line?
0
 
ASPNet_DeveloperAuthor Commented:
Thanks.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.