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

前端 未结 3 1529
旧时难觅i
旧时难觅i 2021-01-05 11:30

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

相关标签:
3条回答
  • 2021-01-05 12:11

    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
    0 讨论(0)
  • 2021-01-05 12:11

    please see this page

    http://msdn.microsoft.com/en-us/library/58wxa9w5(v=vs.100).aspx

    Most importantly this line

    Use when you are working with an existing project or your site targets an older version of IIS, such as IIS 6, and it is not very important that your testing environment match the production environment closely. This server option is the default in Visual Studio. However, the Visual Studio Development Server runs in a different security context than full IIS, and may fail to reveal errors that can occur when you deploy to a production version of IIS.

    The issue is that IISExpress and the local dev server run under your security context. This allows them much more freedom to start processes and have access to files on the local system.

    IIS however runs in a much stricter security context and has a limited access to the machine at hand. Imagine if IIS could do what you are proposing above. you could basically run any arbitrary code on the webserver.

    0 讨论(0)
  • 2021-01-05 12:22

    The intent of your question is still not clear.

    Are you...

    A. trying to connect to a web application (hosted with IIS) from a client browser and access a file from a network share (relative to the server) for the client to consume?

    B. trying to connect to a web application (hosted with IIS) from a client browser and have the application access a file from the network to be used by the server (like a .txt file containing information that the application needs for some processing)?

    C. trying to connect to a web application (hosted with IIS) from a client browser and have the hosted application access a file from a network share (relative to the client) for the server to consume?

    My assumption is you are attempting (B) and if so, you should use System.IO and access your files programmatically instead of trying to launch a process.

    UPDATED BELOW

    If you are trying to connect to a web application, and launch a local process (such as notepad.exe) on the client you cannot do so by launching a process. Otherwise MyVirus.com could launch local processes on my machine and that would be a nightmare. The web application is only going to launch processes on the server, never the client.

    The best you can do is open the file (from the server) and send a Response back to the client with the contents of the file or send the path and file name to the client and open it via javascript or an HTML5 FileReader.

    ADDITIONAL UPDATE

    You should be able to open an image (or other content consumable in a browser) from a UNC path as long as you are your Application Pool Identity has permissions and also, are you using impersonation (identity impersonate="true" userName="someaccount" password="somepassword")?

    ONE LAST UPDATE

    Unfortunately even opening a folder location requires a local process to launch (explorer.exe). That's simply not going to happen with modern browsers for security reasons. You could cook up something wonky like a local windows service on each client machine that checks a database and if it finds x it launches the local explorer.exe to the path stored in the database... but it would have to be checking every second and that sounds like a really bad idea.

    Other than that maybe something like this File Explorer Control from Telerik or this File View Control would serve your purposes. (Disclaimer, I don't know anything about either of these controls just thought they might help).

    0 讨论(0)
提交回复
热议问题