?
Solved

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

Posted on 2011-05-11
24
Medium Priority
?
1,448 Views
Last Modified: 2012-06-22
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.
0
Comment
Question by:ASPNet_Developer
  • 8
  • 8
  • 6
  • +2
24 Comments
 
LVL 10

Accepted Solution

by:
MaxOvrdrv2 earned 2000 total points
ID: 35738196
"file:///" & fileUpload1.PostedFile.FileName
0
 
LVL 4

Expert Comment

by:abhinayp
ID: 35738253
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
 
LVL 10

Expert Comment

by:MaxOvrdrv2
ID: 35738340
how silly of me... try this:

Server.MapPath(FileUpload1.FileName)

that should get it working in FF.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:ASPNet_Developer
ID: 35738349
MaxOvrdrv2: no luck, it is not returning the path.
0
 
LVL 10

Expert Comment

by:MaxOvrdrv2
ID: 35738359
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
 

Author Comment

by:ASPNet_Developer
ID: 35738375
i have already tried Server.MapPath(FileUpload1.FileName), it is not returning the correct path
0
 

Author Comment

by:ASPNet_Developer
ID: 35738388
Both of them
0
 
LVL 10

Expert Comment

by:MaxOvrdrv2
ID: 35738390
hmmm... works fine for me... what OS are you on, and what is the path that you're expecting?
0
 
LVL 10

Expert Comment

by:MaxOvrdrv2
ID: 35738403
oh wait yeah i see what you mean... hold on...
0
 
LVL 10

Expert Comment

by:MaxOvrdrv2
ID: 35738461
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
 
LVL 9

Expert Comment

by:gery128
ID: 35738663
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
 

Author Comment

by:ASPNet_Developer
ID: 35738701
I am converting the image file to bytes
0
 
LVL 10

Expert Comment

by:MaxOvrdrv2
ID: 35738719
then use the stream object of the postedfile... you don't need to have the path for that.
0
 

Author Comment

by:ASPNet_Developer
ID: 35738768
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
 
LVL 10

Expert Comment

by:MaxOvrdrv2
ID: 35738840
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
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 35738845
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
 
LVL 9

Expert Comment

by:gery128
ID: 35743967
@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
 

Author Comment

by:ASPNet_Developer
ID: 35746830
I tried but it is not wroking, how do I mplement this?
_bytes=fileUploadJurisSealImg.FileBytes
0
 
LVL 9

Expert Comment

by:gery128
ID: 35746938
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
 
LVL 9

Expert Comment

by:gery128
ID: 35746948
Dim _byte As [Byte]() = fileUpload1.FileBytes
0
 

Author Comment

by:ASPNet_Developer
ID: 35747198
It is throwing error that 'FILE MEMBER IS NOT A MEMBER OF STRING'
0
 
LVL 9

Expert Comment

by:gery128
ID: 35752094
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
 
LVL 9

Expert Comment

by:gery128
ID: 35752107
You should NOT get any error by using above line. Where are you writing the above line?
0
 

Author Closing Comment

by:ASPNet_Developer
ID: 35771557
Thanks.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Screencast - Getting to Know the Pipeline

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question