How to open a text file on the client ?

  • Thread starter Thread starter Romu
  • Start date Start date
R

Romu

Dear all,
I'm pretty new to ASP.net even I have some experience with .net.

For my company, I'm developping a build system, this build system is made
from 2 parts :
- a set of powershell scripts
- an asp.net web interface

These 2 parts are located in the same computer.

The scripts deals with the network creating and copying files on a network
share without any problem.

I'm using impersonation for the asp.net to be able to launch the scripts and
this works pretty well. My only problem is when I want to be able to display
the scripts logs on the client side, they are text files and I would like to
be able to open these files, ideally, using the shell extensions (notepad,
....).

The results are displayed on the web page through a TreeView, I tried 2 ways
to do that without success :
- by using the OnSelectedChange event, the value of the TreeNodes is the
file path,
- by using the NavigateUrl property

In both case, nothing happens, my computing environment is XP Pro SP3, IIS
5.1, asp.net 3.5 SP1, and I'm testing with Firefox 3.

Any help would be realy appeciated, really.

Thanks in advance.
 
Thanks Mark, here is more information.

-Using the NavigateUrl property:

the property is set like:
buildNode.NavigateUrl = "file://" + new
FileInfo(@"\\server\path\file.log").FullName;

I assumed there is nothing to do to activate the link and open the file as
this is supposed to be an HTTP link.

- Using the OnSelectionChanged callback:

this the node value is set like: buildNode.Value = new
FileInfo(@"\\server\path\file.log").FullName;

then the callback:
try
{
Response.Redirect(@"file://devserver/DEV/Builds/tag.txt");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}

Here the result is a page which displays an error 404, the "catch" condition
is called but I can't analyse the exception because of "optimized code".

So I'm a little bit sticked with this problem.
 
Oups, my mistake, the callback code is wrong, it remains from some tests,
here is the good one, not a huge difference:

try
{
Response.Redirect(BuildsTree.SelectedValue);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}

I know this code won't use the shell extension to open the file, that's just
a first step but it doesn't work. But if you can fully fix my problem, you're
my god.

Thanks.
 
Romu expressed precisely :
Dear all,
I'm pretty new to ASP.net even I have some experience with .net.

For my company, I'm developping a build system, this build system is made
from 2 parts :
- a set of powershell scripts
- an asp.net web interface

These 2 parts are located in the same computer.

The scripts deals with the network creating and copying files on a network
share without any problem.

I'm using impersonation for the asp.net to be able to launch the scripts and
this works pretty well. My only problem is when I want to be able to display
the scripts logs on the client side, they are text files and I would like to
be able to open these files, ideally, using the shell extensions (notepad,
...).

The results are displayed on the web page through a TreeView, I tried 2 ways
to do that without success :
- by using the OnSelectedChange event, the value of the TreeNodes is the
file path,
- by using the NavigateUrl property

In both case, nothing happens, my computing environment is XP Pro SP3, IIS
5.1, asp.net 3.5 SP1, and I'm testing with Firefox 3.

Any help would be realy appeciated, really.

Thanks in advance.

I guess those scripts run on the server and you can catch their output
(or wait for the script to finish and pick up the output file).

It is possible to send a file to the browser but you can't
automatically have it processed by the "default application", for
security reasons.

Send a file to the browser and trigger a "save" dialog:
Response.Clear();
Response.ContentType = "text/plain";
Response.AppendHeader("Content-Disposition",
"attachment;filename=log.txt");
Response.WriteFile(locallogfile);
Response.End();

If you want to show the result immediately, why not show it in the
resulting page as part of the HTML? Fill some Label with the text, but
first replace Environment.Newline with "<br/>".

Hans Kesting
 
Hi Mark,
TO be honest, I don't care, my need is: I want to display the log files on
the client, ideally using the shell extension, if not possible, through the
browser.

Thanks for your help.
 
Thanks Hans,
This works pretty well !! Even if I would prefer to directly open the file,
this is a good working solution.

Thanks again.
 
I forgot to answer the last part of your post.

Displaying the log file in the current HTML page is not satisfying as some
logs are made of 35000 lines.

But displaying the log in a new HTML page could be an elegant solution. I'm
going to search how to do that, but if you have some code examples, feel free
to post them here.

Many thanks for the help.
 
Hi Mark that's what I tried, if I use the NavigateUrl property to store the
link, nothing works when I click on the node, I can see in the browser status
bar the link is the good one but that's all ; and, I also tried to call
Response.Redirect("file://" + BuildsTree.SelectedValue) and a blank page is
displayed showing "Object moved to here", the given link is here again the
good one, when nothing happens when I click on this link. Pretty strange to
me.
 
Answers here under.
Hmm - if you do View State can you see the correct hyperlink...?

Do you mean run something like "string text = (string)
ViewState["NavigateUrl"];"

My problem is I don't know where to run this code as the OnSelectedChanged
is not called if I use the NavigateUrl property. Where should I put this code?
That's never going to work...

Why? As I told, I'm pretty new to ASP.net. Thanks for your help.
 
Hi Mark,
Yes, when watching the source code of the page, I can see the URLs are well
formed, I even can copy/paste them into my notepad and directly open the
files.

But nothing works when I click on the links, or if I choose "Open in a new
tab". (I'm working with Firefox). I suspect a very deep tuning of the IIS
server or security trick from IIS or ASP, but here is the limit of my
knowledge.

The Hans' solution works pretty well, even if it is not the perfect solution.


Mark Rae said:
Answers here under.
Hmm - if you do View State can you see the correct hyperlink...?

Do you mean run something like "string text = (string)
ViewState["NavigateUrl"];"

Apologies - I meant View Source so that you can see the HTML which has been
streamed to the browser...
 
Differences are :

--- your case

<td><img
src="/WebResource.axd?d=0pCN_sjrGAkkBD9syoWq419c8fQwALT3KSA4tPdQdWs1&t=633655923624902904"
alt="" /></td><td style="white-space:nowrap;"><a
class="mpDefault_cphContent_MyTreeView_0" href="text1.txt"
id="mpDefault_cphContent_MyTreeViewt0">File 1</a></td>

So the link is href="text1.txt", and of course, it works

--- my case

<td><div style="width:20px;height:1px"></div></td><td><a
href="file://\\devserver\DEV\Builds\project\build.log"
id="ctl00_pagecontent_BuildsTreen1i" tabindex="-1"><img
src="images/build-ok.gif" alt="" style="border-width:0;" /></a></td><td
class="ctl00_pagecontent_BuildsTree_2" style="white-space:nowrap;"><a
class="ctl00_pagecontent_BuildsTree_0"
href="file://\\devserver\DEV\Builds\project\build.log"
id="ctl00_pagecontent_BuildsTreen1">LATEST</a></td>

Each node is made of a clickable image and a text, both pointing to the same
file, so the 2 href links.

The main difference betwenn your code and mine seems to be your files are
local to your asp server, mines are not.
 
Ok, that closes the thread, I understand the problem. I would like to really
thank you for your advices and patience. I stay with the Hans' solution which
is a pretty elegant workaround.

Thanks again Mark.
 
Back
Top