簡體   English   中英

由Response.End()生成的異常

[英]Exception generated by Response.End()

我讓用戶使用以下代碼下載文件。 它工作正常,但也會產生錯誤,錯誤的根源是Response.End();

錯誤訊息 由於代碼已優化或本機框架位於調用堆棧的頂部,因此無法評估表達式。

下面是我的代碼。 我該如何處理該錯誤,並且此錯誤不會釋放我的資源,這可能導致不必要的內存使用。

我將此用於asp.net網絡表單應用程序。

    try
    {
        string fileName = Request["file_ID"];
        string path = Server.MapPath("~/App_Data/uploads/"+fileName);


        //string = Server.MapPath(strRequest); 
        System.IO.FileInfo file = new System.IO.FileInfo(path);
        if (file.Exists)
        {
            Response.Clear();
            Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
            Response.AddHeader("Content-Length", file.Length.ToString());
            Response.ContentType = "application/"+file.Extension;
            Response.WriteFile(file.FullName);
            Response.End();

            // System.Threading.Thread.Sleep(2000);
           // Page.ClientScript.RegisterStartupScript(this.GetType(), "MyScript", "alert('aa')", true);

        }
        else
        {
            //Page.ClientScript.RegisterStartupScript(this.GetType(), "MyScript", "myWindow.close();", true);
        }
    }


    catch (Exception rt)
    {
         Response.Write(rt.Message);
    }
}

我在看這個解決方案,但是我不確定如何在我的代碼中實現它。

無法評估表達式...在網頁上

更新:

我實際上希望用戶下載文件並使用腳本后面的代碼關閉文件,這些腳本現在已在代碼中注釋。

因此,我不確定如何更好地優化此代碼以處理由response.end語句生成的異常。

我嘗試使用Page.ClientScript.RegisterStartupScript(this.GetType(), "MyScript", "myWindow.close();", true); 但它也不起作用。

謝謝

另一個答案似乎是在說您不能使用Response.End,因為它不必要,或者在您具有當前捕獲的地方添加ThreadAbortException的捕獲

該問題鏈接到另一個建議添加以下內容的問題:

// Sends the response buffer
Response.Flush()

// Prevents any other content from being sent to the browser
Response.SuppressContent = TrueResponse.SuppressContent = True

使用HttpContext.Current.ApplicationInstance.CompleteRequest

或嘗試一下

Response.OutputStream.Flush()
Response.OutputStream.Close()
Response.Flush()
Response.End() 

READ Response.End()被認為有害嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM