gpt4 book ai didi

c# - 如何在内存中创建一个文本文件并在其上写一些东西并在客户端中打开记事本并在其中打开该文件?

转载 作者:太空宇宙 更新时间:2023-11-03 20:39:41 25 4
gpt4 key购买 nike

我如何在内存中创建一个文本文件(Ram -> Save NoWhere)并在上面写一些东西并在客户端浏览器上打开记事本并在其中打开该文本文件并让用户自己保存它? -> 在代码后面

感谢 future 的进步

最好的问候

最佳答案

你不能那样做。
您所能做的就是将文件内容写入正确的 MIME 响应输入 header (例如“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>

关于c# - 如何在内存中创建一个文本文件并在其上写一些东西并在客户端中打开记事本并在其中打开该文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3621471/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com