Read Text file from local and display in browser

Posted by Fundaravi under ASP.NET on 10/31/2012 | Points: 10 | Views : 3813 | Status : [Member] | Replies : 3
Read the one text file from local system and display in browser ,Use Image Button, if we click the image button i need to display the text file data in new tab of browser

Ravindra


Responses

Posted by: Vasanthmvp on: 10/31/2012 [Member] Starter | Points: 25

Up
0
Down
Hi,
You can just set the postbackurl of the image button to the text file name.
<asp:ImageButton ID="imgbtn" runat="server" ImageUrl="" runat="server" PostBackUrl="~/mytextfile.txt" Target="_Blank" />

Here mytextfile.txt is text file with in my local system. Specify the path here at PostBackUrl attribute.
Target=_Blank will let the file to be opened in new window.

Regards,

Awesome Coding !! :)

Fundaravi, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Fundaravi on: 10/31/2012 [Member] Starter | Points: 25

Up
0
Down
Hi Vasanth,

The file is physically located in D or C drive.. not in solution Explorer. Then this case what is the solution .could you please share this. and one more thing .. I want to read the file from remotely not from local.. in this case what is the solution

Ravindra

Fundaravi, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vasanthmvp on: 10/31/2012 [Member] Starter | Points: 25

Up
0
Down
Hi Ravi,
You are absolutely right, a postbackurl checks with in the solution explorer. The Server is accessible till the website folder under which it is running. That's why we include all the files with in our Solution that are necessary to run our application. Referring to other directory files, you will be reported with an alert "Browser doesn't support its address.. because the protocol is not associated with any program."
On click of the image button, you can include a funtion to open the text file:
protected void open(Object sender, EventArgs e)

{
FileStream content = System.IO.File.Open(@"c:\test.txt", System.IO.FileMode.Open); // Directly open the file or copy the file content into another and open.
}

If you want to open a remote file.. Server.MapPath doesn't work as it refers to the local system only. Info on internet says that you can directly
FTP files from .net using FtpWebRequest. or Include the file under webservice to access it on remote.

Using FtpWebRequest:
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://www.contoso.com/output.txt"); // Your file path.
request.Method = WebRequestMethods.Ftp.UploadFile;
// This example assumes the FTP site uses anonymous logon.
request.Credentials = new NetworkCredential ("anonymous","janeDoe@contoso.com"); // Mention the network credentials to look into you shared files.. ftp accessing files
Stream requestStream = request.GetRequestStream();
requestStream.Write(stuff); // write your stuff here
requestStream.Close();

// You can also update a remote file content
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Console.WriteLine("Upload File Complete, status {0}", response.StatusDescription);
response.Close();

This might not be a perfect solution.. as far as i searched the info, can bring you an idea.
If it is helpful to you, mark it as answer.. it might help others.
Regards,

Awesome Coding !! :)

Fundaravi, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response