views:

206

answers:

2

I currently have a TreeView showing a directory in a web page. I don't think you can capture click events on a Node so instead I'm creating the navigation link to the same page which processes a parameter (path).

I've tried a couple things:


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

^^ The code above only really works for Text files (or whatever extension I want to define). ^^


The code below writes the file as a set of characters to the browser.

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

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


Is there a good solution to this I'm just missing? I need to send any file with an option to save it when selected from a TreeView.

Thanks in advance for your help!

+4  A: 

Just got it working I think...

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();
byanity
+2  A: 

This is the best article I could find: http://www.west-wind.com/weblog/posts/76293.aspx

File on disk:

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

Generated file:

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