Environment: .Net 3.5, jQuery 2.1.4
The result is Hello, Worldreturned in the callback, but is there a way to return it as an attachment?
JQuery
function test() {
$.ajax({
type: "POST",
url: "Handler1.ashx",
contentType: "application/json; charset=utf-8",
success: function (data) {
$('#progressBar').hide();
}
});
general handler:
public class Handler1 : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
System.IO.MemoryStream mstream = GetData();
byte[] byteArray = mstream.ToArray();
mstream.Flush();
mstream.Close();
context.Response.Clear();
context.Response.AddHeader("Content-Disposition", "attachment; filename=mytextfile.txt");
context.Response.AddHeader("Content-Length", byteArray.Length.ToString());
context.Response.ContentType = "application/octet-stream";
context.Response.BinaryWrite(byteArray);
}
private MemoryStream GetData()
{
MemoryStream ReturnStream = new MemoryStream();
try
{
Thread.Sleep(5000);
StreamWriter sw = new StreamWriter(ReturnStream);
sw.WriteLine("Hello, World");
sw.Flush();
sw.Close();
}
catch (Exception Ex)
{
throw Ex;
}
return ReturnStream;
}
public bool IsReusable
{
get
{
return false;
}
}
}
Resources
http://www.c-sharpcorner.com/article/downloading-data-as-a-file-from-a-memorystream-using-a-http/
source
share