在asp.net中下载SQL备份文件



我正在尝试下载sql备份文件,但得到错误:无法计算表达式,因为代码已优化或本机框架位于调用堆栈的顶部;附近respone.end ()

protected void btnDownload_Click(object sender, EventArgs e)
{
try
{
string backupDestination = backupPath;
string dbNAme = dbName;
string dateStamp = DateTime.Now.ToString("yy-MM-dd@HHmm");
string backupfile = backupDestination + '\' + dbNAme + " of " + dateStamp + ".bak";
DataTable dt = blu.queryFunction("BACKUP database " + dbNAme + " to disk='" + backupDestination + "\" + dbNAme + " of " + dateStamp + ".Bak'");
WebClient req = new WebClient();
HttpResponse response = HttpContext.Current.Response;
response.Clear();
response.ClearContent();
response.ClearHeaders();
response.Buffer = true;
response.AddHeader("content-disposition", "attachment; filename= " + dbNAme + ".bak");

byte[] data = req.DownloadData(backupfile);
response.ContentType = "application/sql";
response.BinaryWrite(data);
response.End();
}
catch (Exception ex)
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "alertscipt", "swal('Error!','Database Backup Failed." + ex.ToString() + "','warning')", true);
}
}

我看到的一个问题是缓冲区-将response.Buffer = true;更改为false

response.Buffer = false;

对于大文件,这是错误的,因为它放在缓冲区中,但你想直接发送给用户…

还删除catch (Exception ex)以查看代码上的其他问题-ScriptManager.RegisterStartupScript从更改头的那一刻起就没有运行。所以这个问题对你是隐藏的。

另一种更好更正确的方法是创建一个处理程序并从那里下载文件。

如果备份文件存在于运行应用程序的服务器上,则不应该使用WebClient。从本地磁盘读取文件,例如TransmitFile(),这样就足够了——像这样:

string backupDestination = backupPath;
string dbNAme = dbName;
string dateStamp = DateTime.Now.ToString("yy-MM-dd@HHmm");
string backupfile = backupDestination + '\' + dbNAme + " of " + dateStamp + ".bak";
DataTable dt = blu.queryFunction("BACKUP database " + dbNAme + " to disk='" + backupDestination + "\" + dbNAme + " of " + dateStamp + ".Bak'");
HttpResponse response = HttpContext.Current.Response;
response.Clear();
response.ClearContent();
response.ClearHeaders();
response.Buffer = true;
response.AddHeader("content-disposition", "attachment; filename= " + dbNAme + ".bak");
response.ContentType = "application/octet-stream";
response.TransmitFile(backupfile);
response.Flush();

好的,所以72小时后我发现了错误。下载按钮在更新面板内,所以我得到"无法计算表达式,因为代码已优化或本机框架位于调用堆栈的顶部">

protected void btnDownload_Click(object sender, EventArgs e)
{
try
{
string dbNAme = dbName;
string backupDestination = Server.MapPath("~/BackUp");
string dateStamp = DateTime.Now.ToString("yyyy-MM-dd@HH_mm");
string fileName = dbName + " of " + dateStamp + ".Bak";
if (!Directory.Exists(backupDestination))
{
Directory.CreateDirectory(backupDestination);
}
DataTable dt = blu.queryFunction("BACKUP database " + dbNAme + " to disk='" + backupDestination + "\" + fileName + "'");

byte[] bytes = File.ReadAllBytes(Path.Combine(backupDestination, fileName));
// Delete .bak file from server folder.
if (Directory.Exists(backupDestination))
{
Directory.Delete(backupDestination, true);
}
Response.Clear();
Response.Buffer = false;
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/octet-stream";
Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName);
Response.BinaryWrite(bytes);
Response.Flush();
Response.End();
}
catch(Exception ex)
{
string exception = ex.ToString();
}            
}

最新更新