【问题标题】:How to Send a Any File Type through Redirect in ASP.NET?如何在 ASP.NET 中通过重定向发送任意文件类型?
【发布时间】:2009-07-01 12:34:04
【问题描述】:

我目前有一个在网页中显示目录的 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 中选择时,我需要发送任何带有保存选项的文件。

提前感谢您的帮助!

【问题讨论】:

  • 你需要设置Content Type和Disposition。

标签: c# asp.net file treeview download


【解决方案1】:

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

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();

【讨论】:

    【解决方案2】:

    这是我能找到的最好的文章: http://www.west-wind.com/weblog/posts/76293.aspx

    磁盘上的文件

    Response.ContentType = "image/jpeg";
    Response.AppendHeader("Content-Disposition", "attachment; filename=Image.jpg");
    Response.TransmitFile(Server.MapPath("~/images/image.jpg"));
    Response.End();
    

    生成的文件

    Bitmap bmp = GenerateImage();     
    Response.ContentType = "image/jpeg";
    Response.AppendHeader("Content-Disposition", "attachment; filename=Image.jpg");     
    bmp.Save(Response.OutputStream, ImageFormat.Jpeg);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-23
      • 1970-01-01
      • 2014-01-06
      相关资源
      最近更新 更多