Wesley Skeen
Wesley Skeen

Reputation: 8285

Stop the response from writing a file when there is an access denied error

I have the following code

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();                
}

The file still gets downloaded for the client, albeit a corrupted file but is there a way to stop the file from downloading.

Upvotes: 0

Views: 180

Answers (2)

Kevin Main
Kevin Main

Reputation: 2344

As you are catching an UnauthorizedAccessException why not throw a HttpException with a relevant HTTP status code (i.e a 401) -

throw new HttpException(401, ex.Message);

Upvotes: 0

deerchao
deerchao

Reputation: 10544

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")
}

Upvotes: 1

Related Questions