Contact Yart for a relaxed conversation
Your Name * Message *
Your Email *  
Your Phone   
Our Phone:        03 8685 8718 (within Australia)
61 3 8685 8718 (outside Australia)
Contact Yart

Melbourne Content Management and Flash developers

 

for designers, agencies and your website

Resources / Programming / Link to force _save as_
Link to force 'save as'

Want to let user to download a HTML file or a GIF file?

Here is an example that was used on High Country website to let user download MP3 files.

Create a Generic Handler, let's call it Download.ashx

<%@ WebHandler Language="C#" Class="Download" %>

using System;
using System.Web;

public class Download : IHttpHandler {

public void ProcessRequest (HttpContext context) {
string fname = context.Request.QueryString["audio"]; // This would be the filename without extension
if (fname == null) // No query string
{
context.Response.ContentType = "text/plain";
context.Response.Write("Error");
}
else
{
fname = "audio/" + fname + ".mp3"; // Complete the filename/path
string filepath = context.Server.MapPath(fname);

if (!System.IO.File.Exists(filepath)) // File not found
{
context.Response.ContentType = "text/plain";
context.Response.Write("404 File Not Found");
}
else // Let user download the file
{
context.Response.AddHeader("Content-Disposition", "attachment; filename=\"" + System.IO.Path.GetFileName(fname) + "\"");
context.Response.ContentType = "audio/mpeg";
context.Response.WriteFile(filepath);
}
}
}

public bool IsReusable { get { return false; } }

}

Then in the page that contains the link to the mp3, put this link

<a href="Download.ashx?audio=BrightToOmeo/01 Introduction">4.9 MB DOWNLOAD</a>
The latest at Yart
Search: