我正在嘗試下載 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);
}
}
uj5u.com熱心網友回復:
我看到的一個問題是緩沖區 - 將 the 更改response.Buffer = true;為 false
response.Buffer = false;
對于大檔案,這是錯誤的,因為它將它放在緩沖區中,但您想將其直接發送給用戶...
還要洗掉catch (Exception ex)以查看代碼中的其他問題 -ScriptManager.RegisterStartupScript從更改標題的那一刻起就不會運行。所以這個問題對你是隱藏的。
另一種更好、更正確的方法是創建一個處理程式并從那里下載檔案。
uj5u.com熱心網友回復:
如果運行應用程式的服務器上存在備份檔案,則不應使用 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();
uj5u.com熱心網友回復:
好的,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();
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/337977.html
