0

我有以下代码

try
{        
    GetFileAndDisplay() // Gets the file from the server and writes the file in a Response stream
}
catch (UnauthorizedAccessException ex)
{
    Page.ClientScript.RegisterStartupScript(this.GetType(), "Msg", "<script type='text/javascript'>alert('Error')</script>");
    Response.Clear();                
    Response.ContentType = "";               
    Response.End();                
}

该文件仍会为客户端下载,尽管文件已损坏,但有办法阻止文件下载。

4

2 回答 2

1
try
{        
    GetFileAndDisplay() // Gets the file from the server and writes the file in a Response stream
}
catch (UnauthorizedAccessException ex)
{
    Page.ClientScript.RegisterStartupScript(this.GetType(), "Msg", "<script type='text/javascript'>alert('Error')</script>");
    Response.Clear();                
    Response.Redirect("path/to/some/page/that/tells/the/user/something/is/wrong")
}
于 2012-11-07T14:09:40.850 回答
0

当您捕获 UnauthorizedAccessException 时,为什么不抛出带有相关 HTTP 状态代码(即 401)的 HttpException -

throw new HttpException(401, ex.Message);
于 2012-11-07T14:25:02.360 回答