Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Web.HttpException: Response is not available in this context.
Source Error:
Line 10: public void Download(string filename)
Line 11: {
Line 12: Response.ContentType = "application/exe";
Line 13: Response.AppendHeader("Content-Disposition", filename);
Line 14: Response.TransmitFile(Server.MapPath("~/Resources/bb.exe"));
I am having this method inside a class, i call this method when I click a button
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
public class Common: System.Web.UI.Page
{
}
Where am I going wrong
When using the response object from an aspx page, its codebehind class or a user control, the response object is directly available because all these derive from the page object.
When using the response object in your own class, the object is not available, but you can access it:
HttpContext.Current.Response. -> something
You just can't use Response object of the Page like you're doing, because this object represents response which had been sent already, so you cannot modify it or use TransmitFile with it.
You need to create your own handler to write file to server output. Refer to IHttpHandler documentation (http://msdn.microsoft.com/en-us/library/system.web.ihttphandler.aspx)
I'm not sure How,
But a broken connection string in my Web.config file - caused the same issue.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With