Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Opening a text file on the local system from an asp.net web application

Tags:

c#

asp.net

iis

OK to add clarification after the comments posted and the fact I realise my original question was massively confusing. This is what I am trying to achieve....

This will be an web application running on a local intranet and not over the internet. Ultimately I want to be open a network folder location from within the the web application. So for example the web application creates folders on the file server with a set structure i.e:

\server\jobnumber\exhibitreference\image1 \server\jobnumber\exhibitreference\image2

I want the user to be able to navigate to the record and click a link to open it's matching folder location. The users, web server and file server are all on the same domain.

The code below was just used as an example to try and get it working for a file/folder on my local machine before I moved off to trying a remote folder. I appreciate this was confusing.

Original question

I have created .Net/C# web application and I want to open a text file at a specified location. The code below is working fine when run on IIS Express but once published to IIS it does not work.

At present IIS Express and IIS 7 are running on my local machine. The IIS application pool is configured to run under my domain account (had to do this as we have a double hop issue of authentication to SQL server) So far I have the following code:

ProcessStartInfo processStartInfo = new ProcessStartInfo();
processStartInfo.WorkingDirectory = @"C:\Users\pcustance\Desktop\";
processStartInfo.FileName = @"notepad.exe";
processStartInfo.Arguments = "test.txt";
processStartInfo.WindowStyle = ProcessWindowStyle.Maximized;
processStartInfo.CreateNoWindow = true;

Process process = Process.Start(processStartInfo);

Watching the system processes through task manager I can see that the process "notepad.exe" gets created successfully but no window opens. It says the process is running under "pcustance" account but I can only see it when I select "show processes from all users" in task manager.

Is the window not launching because somehow it is being run under the wrong account?

I have also tried:

Process.Start("C:\Users\pcustance\Desktop\test.txt");

As before, this works in IIS Express but not on IIS7.

Any help is greatly appreciated.

Solution

At the moment I have had to resort to using Internet Explorer which supports the use of local links out the box. The browser can be pointed at a network location with the following:

file:///\\server\folder\location
or 
file://///server/folder/location
like image 520
oceanexplorer Avatar asked Dec 03 '13 15:12

oceanexplorer


1 Answers

All your code runs within asp.net which is hosted in a server (via IIS).

The code you have written will execute in the context of where your asp.net app is hosted.

While doing web development using visual studio, the "server" and the "client" (i.e. the browser) is usually the same computer. The code executes in the context of a localized development server. Your browser will make requests to "that" server. Therefore the code you wrote is bound to give you the illusion that you've started the process - notepad.exe

The stuff you've actually implemented about doesn't apply for web applications in general. It isn't even feasible. Since the "server" and "client" are two different machines now. The closest you can get into implementing this requirement is serving up the file as response. To the end user, this is equivalent to downloading (in most cases).

Edit:

Your options are serving up the file as-is shown in the code:

Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", _
  "attachment; filename=""" & filename & """");

This will force user to download the file on the client with a default name as specified by value in filename. Actually you can vary the Content-Disposition part to instruct the browser how to load. However, it depends on the target browser. Here is a small example:

FileStream MyFileStream = new FileStream(@"d:\inetpub\wwwroot\small.txt", FileMode.Open);
long FileSize;
FileSize = MyFileStream.Length;
byte[] Buffer = new byte[(int)FileSize];
MyFileStream.Read(Buffer, 0, (int)MyFileStream.Length);
MyFileStream.Close();
Response.ContentType="text/plain";
Response.AddHeader( "content-disposition","inline; filename=sample.txt");
Response.BinaryWrite(Buffer);

You should research a few articles and experiment. Here are some links to start with:

  1. Content-Disposition:What are the differences between "inline" and "attachment"?
  2. http://www.nullskull.com/articles/20011006.asp
  3. http://www.windowsdevcenter.com/pub/a/dotnet/2002/04/01/asp.html
like image 85
deostroll Avatar answered Nov 07 '22 06:11

deostroll