gpt4 book ai didi

c# - 如何在 ASP.NET 中通过重定向发送任何文件类型?

转载 作者:行者123 更新时间:2023-12-02 05:52:18 24 4
gpt4 key购买 nike

我目前有一个显示网页中目录的 TreeView。我认为您无法捕获节点上的点击事件,因此我创建了指向处理参数(路径)的同一页面的导航链接。

我尝试了几件事:


Response.ContentType = "text/txt";
Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName);
Response.Write(file.ToString());
Response.End();

^^ 上面的代码仅适用于文本文件(或我想定义的任何扩展名)。 ^^


下面的代码将文件作为一组字符写入浏览器。

if (!IsPostBack)
{
string path = Request["path"];

if ((path != "") && (path != null))
{
Response.TransmitFile(path);
Response.End();
}
}

对于我所缺少的这个问题有一个好的解决方案吗?我需要发送任何文件,并提供从 TreeView 中选择时保存该文件的选项。

预先感谢您的帮助!

最佳答案

我认为刚刚开始工作......

System.IO.FileInfo file = new System.IO.FileInfo(path);

Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
Response.AddHeader("Content-Length", file.Length.ToString());
Response.ContentType = "application/octet-stream";
Response.WriteFile(file.FullName);
Response.End();

关于c# - 如何在 ASP.NET 中通过重定向发送任何文件类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1068925/

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