0

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?

dearn44
  • 3,198
  • 4
  • 30
  • 63
  • Check for file path, in my case "Path" is major problem when trying from server – Mayur Patel Jan 25 '16 at 09:07
  • Are you sure it is send, because in the response header content-type is text/html, but in your response content type you wrote text/plain. – mybirthname Jan 25 '16 at 09:11
  • @mybirthname first of all... nice username. Yeah I am sure but I forgot that during my tests I used a header that came from different configurations, I will update it. Hi Mayur, the thing is that I can see the data in my response in chrome, also the variable has indeed the correct value if you print it. – dearn44 Jan 25 '16 at 09:16
  • Can you show us the code, where you write your text to the response stream? – Marco Jan 25 '16 at 09:26
  • @Serv sorry about that, the code was there but i erroneously deleted it in my last edit. – dearn44 Jan 25 '16 at 11:38
  • How do you "use" this code? What _does_ happen? – CodeCaster Jan 25 '16 at 11:39
  • @CodeCaster added the exact class and function. This function is simply called by a function inside my angular factory, gets the file path and sends (tries at least) it back. – dearn44 Jan 25 '16 at 11:54
  • Then show the angular code. – CodeCaster Jan 25 '16 at 12:00
  • Is this an Ajax call? It won't directly work if it is (it can be "hacked away" using `FileApi`, but the browser won't request a filename or save the contents on its own if this is the response to an `XmlHttpRequest`) – Jcl Jan 25 '16 at 12:06
  • Ok also added the client side code, did not realize it was important. – dearn44 Jan 25 '16 at 12:30
  • What do you expect that Angular code to do? Trigger the file download of the browser? Where does its return value go to? – CodeCaster Jan 25 '16 at 12:35
  • @CodeCaster Yes that was the general idea I expected the text file to get downloaded by the browser. At the moment its return value is not used in any way. – dearn44 Jan 25 '16 at 12:52

1 Answers1

0

Try using content type of "application/octet-stream". The browser will treat it as a download file.

Arman Hatefi
  • 43
  • 1
  • 8
  • This has no effect whatsoever Im afraid. – dearn44 Jan 25 '16 at 12:26
  • Please try "application/download", if not working, after googling, it sounds that you should use a hidden form to submit your request to download the file which you can find this in these links : [link#1](http://stackoverflow.com/questions/16086162/handle-file-download-from-ajax-post) , [link#2](http://stackoverflow.com/questions/28165424/download-file-via-jquery-ajax-post), [link#3](https://gist.github.com/DavidMah/3533415), [link#4](http://stackoverflow.com/questions/13519058/download-file-with-ajax-post-request-via-spring-mvc) – Arman Hatefi Jan 26 '16 at 05:53
  • application/download does not do anything I am afraid. But you third link is promising, I will have to test it as soon as I find time. – dearn44 Jan 26 '16 at 09:22