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

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

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.

Dani AI

Generated

— short diagnosis and a few practical next steps that complement the replies from and .

The HttpException "Response is not available in this context" means your code is running where the ASP.NET Response object (HttpContext.Current.Response) is not present or not usable — common causes are background threads, timer callbacks, Application_Start, or certain unload/async stages of the page lifecycle. If the code that builds or redirects to the WAV runs off the request thread, any call that touches Response will throw that exception. (stackoverflow.com)

The Server.Transfer suggestion from only works for transferring to another page on the same server (a server-local virtual path) — it will not accept a full external URL like "". For external URLs you must let the browser navigate (client-side) or issue a normal redirect from a valid request context. (learn.microsoft.com)

If the WAV lives on your server and you want the browser to download it automatically, the usual pattern is to expose a dedicated download endpoint/handler that sets a Content-Disposition: attachment header and streams the file (for large files prefer TransmitFile so IIS can stream efficiently). That is the same general approach outlined but implemented as a distinct download URL (download.aspx / .ashx) the client can hit when the file is ready. (learn.microsoft.com)

Troubleshooting checklist (minimal changes to your app):

  • Confirm the code that calls Response is running in the request thread (test for HttpContext.Current).
  • If the WAV generation runs asynchronously, have that worker save the file and return a URL; then use client-side navigation or an AJAX callback to send the browser to that URL.
  • If the file is local, serve it from a download handler that sets Content-Disposition and streams (TransmitFile/WriteFile).

Quick checks you can drop in while debugging:

' check whether a request context exists
If HttpContext.Current Is Nothing Then
    ' running outside a request — cannot call Response here
End If
// client-side redirect (works for external URLs)
window.location.href = "http://a.b.com/1.wav";

If those checks show the call is outside the request, change the flow so the request thread returns a page (or JSON) containing the download URL and let the browser navigate to it. For details on headers/streaming see the linked docs above. (learn.microsoft.com)

Recommended Answers

All 3 Replies

Hi,
Try to use Server.Transfer("")

<%@ 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>

try this link....good one...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.