I want to be able to download a text file that is saved somewhere in my server.
I am trying to send the file as an attachment like this:
public class Download: IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.Clear();
context.Response.ContentType = "text/plain";
context.Response.AddHeader("Content-Disposition", "attachment; filename=YourData.txt");
context.Response.WriteFile(@fullPath); //use your file path here.
context.Response.Flush();
context.Response.End();
...
The way this is called is by using a simple ajax call like this:
return $.ajax({
method: 'POST',
url: 'http://' + location.host + '/Download.ashx',
data: senddata
});
If I try to use this no visible changes happen. Nonetheless my data is indeed sent from the server as I can confirm from chromes' console. More so the response header looks like this
Cache-Control:private
Connection:Close
Content-Disposition:attachment; filename=YourData.txt
Content-Type:text/plain
Date:Mon, 25 Jan 2016 09:17:15 GMT
Server:ASP.NET Development Server/10.0.0.0
Transfer-Encoding:chunked
X-AspNet-Version:4.0.30319
Why isnt my file downloaded as a .txt attachment?