18

I need to force the initiation of download of a .sql file, when user clicks a button in my ASP .NET (C#) based web application.

As in, when the button is clicked, a save as dialog should open at the client end...

How do I do this?

EDIT

This is the code I am using

        string sql = "";
        using (System.IO.StreamReader rdr = System.IO.File.OpenText(fileName))
        {
            sql = rdr.ReadToEnd();
        }
        Response.ContentType = "text/plain";
        Response.AddHeader("Content-Disposition", "attachment; filename=Backup.sql");
        Response.Write(sql);
        Response.End();

This is the error that I'm getting...

alt text http://img40.imageshack.us/img40/2103/erroro.gif

What's wrong?

Sameet
  • 2,191
  • 7
  • 28
  • 55
  • You shouldn't be doing it like that in button's click event. Use Response.Redirect or other methods to redirect the user to another page in button click event. See the instruction in my answer. – Mehrdad Afshari May 16 '09 at 22:00

5 Answers5

29

Create a separate HTTP Handler (DownloadSqlFile.ashx):

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

using System;
using System.Web;

public class DownloadHandler : IHttpHandler {
    public void ProcessRequest(HttpContext context) {
        var fileName = "myfile.sql";
        var r = context.Response;
        r.AddHeader("Content-Disposition", "attachment; filename=" + fileName);
        r.ContentType = "text/plain";
        r.WriteFile(context.Server.MapPath(fileName));
    }
    public bool IsReusable { get { return false; } }
}

Then make the button in your ASP.NET page navigate to DownloadSqlFile.ashx.

Mehrdad Afshari
  • 414,610
  • 91
  • 852
  • 789
18

You need to tell the browser that what you are sending is not html and shouldn't be displayed as such in the browser. The following code can be used to return some text in your server-side code and it will present the save dialog to the user as you wanted:

Response.Clear(); //eliminates issues where some response has already been sent
Response.ContentType = "text/plain";
Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}.sql", filename));
Response.Write(yourSQL);
Response.End();

filename: the name you wish to give it

yourSQL: the content of the sql file

sshow
  • 8,820
  • 4
  • 51
  • 82
Neil Trodden
  • 4,724
  • 6
  • 35
  • 55
  • getting an error on doing this. added a screenshot in the question post. – Sameet May 16 '09 at 20:49
  • I'm copying that code out of a page I use that downloads .csv data so I know it works. I call it in the page_load event, perhaps your page has already started sending data back to the client? – Neil Trodden May 16 '09 at 20:57
  • My code is in the button_click event...why does it need to be in page_laod()? I need the download to start on a button click event... – Sameet May 16 '09 at 21:06
  • 1
    Look at Mehrdad's comment on your question above, he is right do a response.redirect to a dedicated page. I realised that the page that I use to do the download simply looks at the request string to work out the id of the item I need to return and it has no controls on the page at all. See also the answer from mehrdad who gives a much better answer than mine - a httphandler is better practise. – Neil Trodden May 16 '09 at 22:09
5

The problem is that you're using the code on the same page as is currently loaded in the browser. You are attempting to modify the response stream and disposition of the current loaded page. Instead, create a separate HttpHandler, as suggested by Mehrdad. Then on the click of the button, you will invoke the URL to the handler. The button could even be a simple hyperlink that had the following as the source URL:

<a href="DownloadSqlFile.ashx">Download SQL</a>

Make sense? The point being, you cannot modify the response of the already loaded page. Launch a new request using a handler to do the work.

Ryan Farley
  • 11,315
  • 4
  • 46
  • 43
1

Add a Response.Clear before you Response.Write to fix the error, if stated properly in the Chrome dialog.

So to integrate in the snippet in other answer...

//add this
Reponse.Clear(); 

//from other answer
Response.ContentType = "text/plain";
Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}.sql", filename));
Response.Write(yourSQL);
Response.End;
oglester
  • 6,605
  • 8
  • 43
  • 63
0

The solution below works for me

string fileNameAndExtension = "thisFile.pdf";     
string fullPath = "../folder/files/" + fileNameAndExtension;       
Response.Clear();
Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileNameAndExtension );
Response.TransmitFile(fullPath);
Response.End();
gengisdave
  • 1,969
  • 5
  • 21
  • 22