File.OpenText method is not declared error - asp.net

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())
{
.....
}

Related

Not able to download file name containing 'aspx' character in asp .net

Am developing asp .Net web application.
In one of my aspx file am using file to download using generic handler. Everything works great. But when am testing i felt a strange problem. That, if am uploading a image or document with file name containing aspx character for Eg; aspxphoto as file name.
Uploading doesnt have any any problem but when i try to download it is throwing error in Generic handler file as
Object reference not set to an instance of an object.
Can anyone help me why this problem happends and how can i fix it?
You will not be able to do this. The IIS handler wants to "handle" the ASPX. You should simply not allow it, or if you have to, rename it to .aspx.uploaded or something. If you allowed, it you could open yourself to hacking.
As another option, you may be able to create a virtual that implements ("no processing") - possibly using the HTTP Handler under the virtual and just disabling script / execute permissions (under handler >> Edit Feature Permission >> Script OR under Virtual >> Edit permissions >> Special >> Turn Execute off.
I would not recommend the last, since it will add complexity when migrating between test and live AND for recovery (DR).

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

FileUpload control (UploadButton.PostedFile.FileName)

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

Browse for file window without uploading the file

Is there a way to get the select file dialog box open and putting the location of the file into a textbox without ever uploading the file?
ETA I'm using VB.NET in a web page. By using the asp:fileupload tag I can get the file location
_fudFileLocation.PostedFile.FileName_
But how do I prevent the file from being uploaded at all. We don't need it, just the file location. (The files are on a shared drive so if it's M:\documents\todayslunch.pdf for person A, it's the same for person B.)
You do not get access to the full filepath in the browser because of security.
If this was possible, one could get the full layout of the computer of anyone going to any website.
System.Web.HttpPostedFile.FileName gets the fully qualified name of the file on the client which includes the directory path.

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.

Resources