My question is simple: i wrote a program that will allow my user to enter some text and it will produce a wav file based on the input and allow them to download it. I am now in the stage that writting the redirect code to redirect the user to download their wav file.

I use VB (asp.net) and use the following code:

Response.Redirect("http://a.b.com/1.wav")

but i end up with:

Server Error in '/' Application.
--------------------------------------------------------------------------------

Response is not available in this context.
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:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:


[HttpException (0x80004005): Response is not available in this context.]
Microsoft.VisualBasic.CompilerServices.Container.InvokeMethod(Method TargetProcedure, Object[] Arguments, Boolean[] CopyBack, BindingFlags Flags) +272
Microsoft.VisualBasic.CompilerServices.NewLateBinding.CallMethod(Container BaseReference, String MethodName, Object[] Arguments, String[] ArgumentNames, Type[] TypeArguments, Boolean[] CopyBack, BindingFlags InvocationFlags, Boolean ReportErrors, ResolutionFailure& Failure) +196
Microsoft.VisualBasic.CompilerServices.NewLateBinding.LateCall(Object Instance, Type Type, String MemberName, Object[] Arguments, String[] ArgumentNames, Type[] TypeArguments, Boolean[] CopyBack, Boolean IgnoreReturn) +216
_Default.Button1_Click(Object sender, EventArgs e) +180
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +105
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +107
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +7
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +11
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5102

i have also tried:

Response.Status = "301 Moved Permanently"
Response.AddHeader("Location", "http://a.b.com/1.wav")

but i also endup with:

Server Error in '/' Application.
--------------------------------------------------------------------------------

Response is not available in this context.
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:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:


[HttpException (0x80004005): Response is not available in this context.]
Microsoft.VisualBasic.CompilerServices.Container.InvokeMethod(Method TargetProcedure, Object[] Arguments, Boolean[] CopyBack, BindingFlags Flags) +272
Microsoft.VisualBasic.CompilerServices.NewLateBinding.CallMethod(Container BaseReference, String MethodName, Object[] Arguments, String[] ArgumentNames, Type[] TypeArguments, Boolean[] CopyBack, BindingFlags InvocationFlags, Boolean ReportErrors, ResolutionFailure& Failure) +196
Microsoft.VisualBasic.CompilerServices.NewLateBinding.LateCall(Object Instance, Type Type, String MemberName, Object[] Arguments, String[] ArgumentNames, Type[] TypeArguments, Boolean[] CopyBack, Boolean IgnoreReturn) +216
_Default.Button1_Click(Object sender, EventArgs e) +180
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +105
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +107
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +7
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +11
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5102


--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:2.0.50727.42; ASP.NET Version:2.0.50727.42

I pretty sure that the address is correct and can be download directly from the browser, but i just can't use it in the program. Can you please help me to solve this problem as it really gives me headache. Many thanks.

<%@ Page language="vb" runat="server" explicit="true" strict="true" %>
	<script language="vb" runat="server">
	Sub Page_Load(Sender As Object, E As EventArgs)
	    Dim strRequest As String = Request.QueryString("file") '-- if something was passed to the file querystring
	    If strRequest <> "" Then 'get absolute path of the file
	        Dim path As String = Server.MapPath(strRequest) 'get file object as FileInfo
	        Dim file As System.IO.FileInfo = New System.IO.FileInfo(path) '-- if the file exists on the server
	        If file.Exists Then 'set appropriate headers
	            Response.Clear()
	            Response.AddHeader("Content-Disposition", "attachment; filename=" & file.Name)
	            Response.AddHeader("Content-Length", file.Length.ToString())
	            Response.ContentType = "application/octet-stream"
	            Response.WriteFile(file.FullName)
	            Response.End 'if file does not exist
	        Else
	            Response.Write("This file does not exist.")
	        End If 'nothing in the URL as HTTP GET
	    Else
	        Response.Write("Please provide a file to download.")
	    End If
	End Sub
	</script>
This article has been dead for over six months. Start a new discussion instead.