FileUpload control (UploadButton.PostedFile.FileName) - asp.net

Recently i developed a code on asp that requires to upload a file up to a server.
As i found out from the web, in order to view the local file of a file it can be done by doing UploadButton.PostedFile.Filename.
string fileName = UploadButton.PostedFile.Filename;
This will show the whole local path(eg. C:\Documents and Settings\christopher.lim\Desktop\HelloWorld.txt).
This work fine if it is run the code on my desktop (where my PC is the server itself) but when i shifted the code over to a test server and tried it on my desktop(PC is the client), it only display my file name instead of the whole path.
Example:
string fileName = UploadButton.PostedFile.Filename;
Response.Write("FileName: " + fileName);
1) Local PC -> C:\Documents and Settings\christopher.lim\Desktop\HelloWorld.txt
2) Test Server -> HelloWorld.txt
P/S: Sorry if it's confusing because i am new to server client. Correct me if i'm wrong.

As per MSDN-
The file name that the FileName property returns does not include the
path of the file on the client.
While it is true that on a local system you can get the complete path, but while you run it on a server it will only return the name of the file.
Also FYI the file upload control behaves differently in different browsers. In firefox you ill probably get only the file name and not the full path using fileupload.postedfile.filename and in IE the same thing may show you the full path.
However the path of the file uploaded from client system shouldn't matter as only the file name is more than enough, but if you still have the need try- Path.GetFileName(filename) MSDN link

Related

Get file path & filename with asp:FileUpload, don't want file... just path and name

I am developing a .NET intranet site which will enable the user to see a list of files (file details stored in DB) and link to the actual PDF/XML/XLS and open it... kind of like a table of contents for the network.
During data entry, the user enters various data about a document, then browses to the file on the network and selects it using the asp:FileUpload. The codebehind then saves the network path to the DB. There is alot of overhead here because i'm sending the file to the server but never use it.
Everything has been working fine until someone tries to use a large PDF file then I get the dreaded MAXIMUM REQUEST LENGTH EXCEEDED error... So I'm trying to find a solution here... I do not need the actual file.. just the path and filename.
I know not all browsers send the full path but our systems have older browsers so everything is working fine now, but will probably break soon.. which is another reason to find a different solution.
I've looked into Javascript to pull the path but that won't work...
Any other ideas? Other ways to just grab the path and filename? (besides manually typing it in to a Text field)
Thanks,
Todd.
This may help too
How to get the full path of a file from asp: file upload?
string filename = Path.GetFileName(FileUpload1.FileName);//file name
string path= Server.MapPath(filename);//path

ASP.net file upload from a non server - issue with Server.MapPath?

I am trying to upload a file in asp.net using the following code
Dim FileName As String = System.IO.Path.GetFileName(ClientFileName)
MyFile.PostedFile.SaveAs(Server.MapPath("~/UploadedImportedFiles/" + FileName))
if the file being uploaded (say book1.xls) resides on the machine that is also the server all works perfectly, but if the file resides on a Pc that is not the server it fails on the second line. I think the problem is that Server.MapPath seems to refer to the non server PC when it is uploaded from there.
Thanks
You wrongly getting file name. You should use below code
string filename = Path.GetFileName(FileUploadControl.FileName);
Of course change control name to your own.
Please See: http://msdn.microsoft.com/en-us/library/aa479405.aspx

ASP .NET invalid path

I have a web page which prompts the user for an excel file using the fileupload control. What it then does is read the file into a datatable using an OleDbConnection and then runs other code with that data. When I test in Visual Studio, it works fine. For example, I can look up a file 'g:\myfiles\upldtest.xls', it finds the file, reads it and the code works. When I try to run it on our web server, I get an error when it tries to create an OleDBConnection saying It is trying to create an OleDbConnection and the path 'g:\myfiles\upldtest.xls' is invalid.
I tried to use ManagementObjectSearcher to convert the connection string path to UNC (\\MyDataServer\myfiles instead of g:\myfiles). When I test it, it shows the correct path but when I upload the page to the web server, I still get the path 'g:\myfiles\upldtest.xls' is invalid.
The code I use to determine the required file name is this
string tname = FileUpload1.PostedFile.FileName; //the file name and path
string gname = tname.Substring(tname.LastIndexOf("\\") + 1); //The path name
Any ideas what I am missing? My contract requires me to use VS2005 and .NET framework 2.0 so, I can't use anything newer. Thanks in advance for the assistance.
HttpPostedFile.FileName returns the fully qualified name of the file on the client machine.
You need to call SaveAs() to actually save the file on the server:
using System.IO;
string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
string filepath = Path.Combine(#"X:\Your\Own\Upload\Folder", filename);
FileUpload1.PostedFile.SaveAs(filepath);
// Now use `filepath` as your data source.
IIS might have already written the file in a temporary location to save memory, but since you can't (and shouldn't) access that location, it makes no difference.
You should also be aware of cross-browser issues. IE sends the whole path to the server on file upload, while Firefox/Chrome do not.

How to download a file from a UNC mapped share via IIS and ASP

I am writing an ASP application that will serve files to clients through the browser. The files are located on a file server that is available from the machine IIS is running on via a UNC path (\server\some\path).
I want to use something like the code below to serve the file. Serving files that are local to the machine IIS is running on is working well with this method, my trouble is being able to serve files from the UNC mapped share:
//Set the appropriate ContentType.
Response.ContentType = "Application/pdf";
//Get the physical path to the file.
string FilePath = MapPath("acrobat.pdf");
//Write the file directly to the HTTP content output stream.
Response.WriteFile(FilePath);
Response.End();
My question is how I can specify a UNC path for the file name. Also, to access the file share I need to connect with a specific username/password.
I would appreciate some pointers on how I can achieve this (either using the approach above or by other means).
I'm not an ASP guy so I might be completely wrong with these answers.
Regarding the path, I don't think you should be using MapPath, since that's to get a relative path and you already know the physical path so can't you just change that to:
string FilePath = #"\\Server\Directory\FileName.txt";
Regarding the account, I think you need to use impersonation, this link seems to discuss just this:
http://aspalliance.com/336_Upload_Files_Using_ASPNET_Impersonation_and_UNC_Share.all

File.OpenText method is not declared error

I have a FileUpload control from which I need the path of a text file. After selecting the file, I need to open and read the data from the text file. For this, I used the following code to open the text file.
fp = File.OpenText(FileUpload2.PostedFile.FileName);
This is working fine on my system. The FileUpload2.PostedFile.FileName property gives the full path of the file. The File.OpenText(() method opens the selected file. But when I run my project in IIS, it gives the following error:
"File.OpenText is not declared."
The FileUpload2.PostedFile.FileName property is not retrieving the full path. It retrieves only the file name. What could be the reason?
This is a typical client server issue. On your system it works, because you are the client and the server, but on IIS (I assume you mean IIS on a test/production server) it looks for the file on the IIS server system while you select the file on your system.
You should use the FileUpload2.PostedFile.InputStream property instead of the filename property.
File f = new File("x.txt");
if(f.exists())
{
.....
}

Resources