当用户单击基于ASP.NET(C#)的Web应用程序中的按钮时,我需要强制下载.sql文件。
当在按钮被点击时,保存为对话框应该在客户端打开…
我该如何做?
编辑
这是我正在使用的代码
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();
这是我正在收到的错误…
alt text http://img40.imageshack.us/img40/2103/erroro.gif
怎么了?
解决方法
创建一个单独的HTTP处理程序(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; } } }