I am working on a web application which uses the FileUpload control. I have an xls file in the full filepath 'C:\Mailid.xls' that I am attempting to upload.
When I use the command
FileUpload1.PostedFile.FileName
I cannot get the full filepath from my system. However, when I use the above command in another system it works fine.
I also tried the following commands with no success:
System.IO.Path.GetFullPath(FileUpload1.PostedFile.FileName); Path.GetFileName(FileUpload1.PostedFile.FileName); System.IO.Path.GetDirectoryName(FileUpload1.PostedFile.FileName).ToString(); Convert.ToString(System.IO.Directory.GetParent(FileUpload1.PostedFile.FileName));
How can I get full path?
The simple solution for preventing file loss from an upload control on postback is to put the upload control outside of an update panel control on a . aspx page. Or, in other words, put all the input controls that might trigger a postback inside an update panel.
asp:FileUpload gets rendered as input type="file" and it does not have that option. You can however do this simple workaround. That is, place a textbox below the FileUpload and prompt the user to enter the desired file name there.
It's currently true that "when you upload a file the browser will only send the source filename and not the full path" - it makes perfect sense that the server has no business knowing whether the file was in "C:\WINDOWS\" or "F:\SOMEDIR\OTHERDIR\PERSONALINFO\". The filename is always sent, and is useful both to help the user 'recognise' the content and possibly to interrogate the file extension to help determine the file type.
However I know from experience that Internet Explorer definitely used to (in older versions) send the entire path. It's difficult to find an authoritative confirmation (except this apache fileupload control doco)
Internet Explorer provides the entire path to the uploaded file and not just the base file name
Regardless, you should not use nor expect the full path to be sent by any 'modern' browser.
Perhaps you misunderstand the way FileUpload
works.
When you upload a file, it is effectively being transferred from the client's computer to the server hosting your application. If you're developing the application, most times, both client and server are the same machine (your computer). Once the application is deployed however, there could be any number of clients connecting to the server, each uploading a different file.
Knowing the full path of the file on the client's computer usually isn't necessary - you'll often want to do something with the file contents. Your examples seem like ASP.NET C#, so I'm guessing you're using the FileUpload control. You can get at the uploaded file's contents by reading the raw stream (FileUpload.PostedFile.InputStream
) or by saving the file first (FileUpload.PostedFile.SaveAs
), then accessing the saved file. It's your responsibility to save the file, if you want it to be accessible after the current request - if you don't, ASP.NET deletes it.
One more thing - don't forget to set the enctype
property on your form to "multipart/form-data". If you don't, the client's browser won't send the file, and you'll spend quite a few minutes wondering what went wrong.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With