Question

Now... I could do this is a separate page, but i don't want to!

I am trying to create a service which fetches a load of JS files required for a project, compresses them and downloads a single file, but what it does isn't really the issue, the downloading bit is not behaving as expected.

From many examples on here, including: In ASP.NET, how to get the browser to download string content into a file? (C#)

I have the (simplified) code:

Sub Download() Handles button.click
    var AllTheScript as string ="blah blah blah"
    Response.Buffer = True
    Response.Clear()
    Response.ClearContent()
    Response.Write(AllTheScript)
    Response.ContentType = "application/text"
    Response.AddHeader("content-disposition", "attachment; filename=myJavascript.js")
    Response.Flush()
End Sub

Obviously, I also have an <asp:button id="button" runat="server"/> element in my page.

When I click this button, it does all the fetch work (not illustrated above) creates the text file and downloads it... excellent...

Except, at the bottom of the JS file it downloads myJavascript.js it has all the markup from the page in which the button was.

I was under the impression that Response.ClearContent() should remove all this?

What am I doing wrong? Or does this have to be a separate page?

Was it helpful?

Solution

In case anyone else comes across this issue again, I solved the problem, very simple!

All it needed was

Response.End()

At the end!

So the full function becomes:

Sub Download() Handles button.click
    var AllTheScript as string ="blah blah blah"
    Response.Buffer = True
    Response.Clear()
    Response.ClearContent()
    Response.Write(AllTheScript)
    Response.ContentType = "application/text"
    Response.AddHeader("content-disposition", "attachment; filename=myJavascript.js")
    Response.Flush()
    Response.End() '///<<< Final Line Essential!
End Sub

OTHER TIPS

Try this

var context = HttpContext.Current;

context.Response.Clear();

context.Response.ClearHeaders();

context.Response.ClearContent(); ////

context.Response.AddHeader("content-disposition", "attachment; filename=myJavascript.js");

content.Response.Write(AllTheScript);

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top