1
如何在内存中制作文本文件(Ram - >保存NoWhere)并在其上写上一些内容并在客户端浏览器上打开记事本并打开该文本文件并让用户通过他/她自己保存该文件? - >在如何在内存中制作文本文件并在其上写入内容并在客户端打开记事本并打开该文件?
感谢在未来提前
问候
如何在内存中制作文本文件(Ram - >保存NoWhere)并在其上写上一些内容并在客户端浏览器上打开记事本并打开该文本文件并让用户通过他/她自己保存该文件? - >在如何在内存中制作文本文件并在其上写入内容并在客户端打开记事本并打开该文件?
感谢在未来提前
问候
你不能这样做,后面的代码。
您只需将文件内容写入Response,并在头中输入正确的MIME类型(例如“text/plain”),客户端浏览器将使用配置的查看器为提供的MIME类型打开文本数据。
<%@ 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
在这里被稍微修改C#示例代码
<%@ 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>
我发现下面的代码: MemoryStream的MS =新的MemoryStream(); StreamWriter sw = new StreamWriter(ms); sw.WriteLine(“This Is A Test Text”); System.Diagnostics.Process.Start(“notepad.exe”,“FileNmae”); 但System.Diagnostics.Process.Start需要一个文件名... 我该怎么做这项工作? thnaks – LostLord 2010-09-01 19:39:55
如何在评论中添加新行? – LostLord 2010-09-01 19:40:49
@LostLord你的代码将在服务器上运行nodepad,而不是客户:) – 2010-09-01 19:42:09