views:

64

answers:

1

Hi My Dear Friends :

How can i Make A Text File In Memory(Ram -> Save NoWhere) And Write Something On It And Open NotePad on top of Client browser And Open That Text File In It And Let the user save it by him/her self? -> in code behind

thanks in future advance

best regards

+4  A: 

You can't do that.
All you can do is write content of the file to Response with correct MIME type in header (for example "text/plain") and client browser will open text data with configured viewer for provided MIME type.

<%@ Page language="vb" runat="server" explicit="true" strict="true" %>
2   <script language="vb" runat="server">
3   Sub Page_Load(Sender As Object, E As EventArgs)
4       Dim strRequest As String = Request.QueryString("file") '-- if something was passed to the file querystring
5       If strRequest <> "" Then 'get absolute path of the file
6           Dim path As String = Server.MapPath(strRequest) 'get file object as FileInfo
7           Dim file As System.IO.FileInfo = New System.IO.FileInfo(path) '-- if the file exists on the server
8           If file.Exists Then 'set appropriate headers
9               Response.Clear()
10              Response.AddHeader("Content-Disposition", "attachment; filename=" & file.Name) ' comment this line if needed
11              Response.AddHeader("Content-Length", file.Length.ToString())
12              Response.ContentType = "application/octet-stream" 'this is MIME type
13              Response.WriteFile(file.FullName)
14              Response.End 'if file does not exist
15          Else
16              Response.Write("This file does not exist.")
17          End If 'nothing in the URL as HTTP GET
18      Else
19          Response.Write("Please provide a file to download.")
20      End If
21  End Sub
22  </script>

http://www.xefteri.com/articles/show.cfm?id=8

Here is slightly modified c# sample code

<%@ Page Title="Home Page" Language="C#" AutoEventWireup="true"%>
<script language="cs" runat="server">
    public void Page_Load(object sender, EventArgs e)
    {

        byte[] buffer;
        using (var memoryStream = new System.IO.MemoryStream())
        {
            buffer = Encoding.Default.GetBytes("Hello StackOverflow"); //Dummy data
            memoryStream.Write(buffer, 0, buffer.Length);
            Response.Clear();
            Response.AddHeader("Content-Disposition", "attachment; filename=hello.txt"); //This wil force browser to silently download file. you can comment this line to see difference
            Response.AddHeader("Content-Length", memoryStream.Length.ToString());
            Response.ContentType = "text/plain"; //This is MIME type
            memoryStream.WriteTo(Response.OutputStream);
        }
        Response.End();

    }
</script>
Sergey Mirvoda
i found the below code : MemoryStream ms = new MemoryStream(); StreamWriter sw = new StreamWriter(ms); sw.WriteLine("This Is A Test Text"); System.Diagnostics.Process.Start("notepad.exe", "FileNmae");but System.Diagnostics.Process.Start Needs A File Name...how can i do this job?thnaks
LostLord
How can i make new line in comments?
LostLord
@LostLord your code will run nodepad on server, not on client :)
Sergey Mirvoda
can u give a c# code?
LostLord
@LostLord Sample updated
Sergey Mirvoda
woh - that is working perfectly / but can u explain more about that? (I AM REALLY REALLY SORRY - THANKS FOR YOUR ATTENTION AND YOUR CONVERT)
LostLord