hyperlink to a text file in asp.net

G

Guest

Hi,

I have a webform and I want to create a hyperlink to a text file, so that it
opens on internet explorer in a new window.
I have added the following
<a href="file:///D:/Test/Test.txt" id="hplTest" runat="server"
testhyperlink</a>

Now when I click on the hyperlink, nothing is happening. No error.
when I open a browser and type file:///D:/Test/Test.txt, the file is opening.

I would appreciate if anybody could help me out.
Thanks,
Sohan
 
B

Bruce Barker

browser security prevents a website from opening a file on the local disk

-- bruce (sqlwork.com)
 
R

Raymond

That would've worked a few years ago, but no more.
One of the IE security patches from a couple of years
ago removed that capability.
 
G

Guest

So is there any other way to open a text file from local disk in internet
explorer in a webform?

Thanks,
Sohan
 
R

Raymond

You can prompt the user to send you
the local file using a file input form field in
a form submission.

There is also something where you can write to
and then retrieve some small amounts of data, other than
in cookies, called IsolatedStorage.

As for arbitrarily sending the local path of that file from the
server and have it open automatically in IE, that's a no no.
 
G

Guest

Prabably my question is not very clear. I think you are assuming that the
files are in user's machine. Its not. I will try to explain.
The text files are in the local folder of the server. I dont want to copy
the files to the folder where website is located. I want to provide some
feature where user can open those files and view them.
How can this be done.

Thanks,
Sohan
 
R

Raymond

Well that's different. You can do something like this:


private void Page_Load(object sender, System.EventArgs e)
{
System.IO.StreamReader sr = new System.IO.StreamReader("c:\\test.txt");
Response.ContentType = "text/plain";
System.IO.TextWriter tx = Response.Output;
tx.Write(sr.ReadToEnd());
sr.Close();
}
 
G

Guest

This works. Thanks.
I also want to do the following:
We have a webgrid (Infragistic - ultrawebgrid) on a Asp form (.net 2005),
and there is a column which will be populated with file names. On clicking on
a cell in the column, we need to open that file in internet explorer.

The files are located at D drive on the server. So basically we need to
append the absolute path to that column value(which I am able to do) and then
open that file in internet explorer in a new window(which I am not able to
do).
Could you help me on this please
Thanks,
Sohan
 
R

Raymond

Add an anchor tag control to that column, set it's target attribute
to "_blank", it's href to an aspx page that will stream the
text data, as in the previous code sample I wrote, and pass the
file name via a querystring in the href URL to that page. Either that or
instead of using "_blank", add a client-side onclick handler for the
anchor tag, use window.open() to open the new window, and return
false to the onclick event.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,816
Latest member
nipsseyhussle

Latest Threads

Top