So, what I want to do is add a link to a web-page which, when clicked, will call a generic handler that will create the required data in memory and serve it back to the user as a stream of some sort, who is then prompted to Save it as a file.
SO, we might have: <a onclick=”makeme()”>Clickety</a>
and the JavaSctipt makeme() function is just
function makeme() {
$.ajax({
url: 'makefile.ashx',
cache: false
}).done(function (response) {
//alert(repsonse)
});
}
In the handler, I've tried:
Dim rtn As String = "1,2,3,4,5"
Dim bArray As Byte() = System.Text.Encoding.Default.GetBytes(rtn.ToCharArray())
' or Dim mstream As New MemoryStream(bArray) ‘ alt (see below)
' either of the next two lines
context.Response.ContentType = "application/octet-stream"
context.Response.ContentType = "application/force-download"
context.Response.AddHeader("Content-Length", bArray.Length.ToString())
context.Response.AddHeader("Content-Disposition", "filename.csv")
context.Response.BinaryWrite(bArray)
' or context.Response.Write(mstream) ‘ if alt used above
Nothing appears to happen though....
even simpler, this:
Dim rtn As String = "1,2,3,4,5"
context.Response.Clear()
context.Response.ContentType = "text/csv"
context.Response.AddHeader("Content-Disposition", "attachment;filename=myfilename.csv")
context.Response.Write(rtn)
also though, nothing happens on clicking the link - I mean, the handler is called, but that's it.