1

I need to initiate file download from ASP.NET page from javascript code.

What would be my best option? Is it possible to call a webservice to do so, or somehow call usual event handler of C# page?

Note that I will retrieve a large amount of data, many megabytes.

onkami
  • 8,791
  • 17
  • 90
  • 176
  • See this post: http://stackoverflow.com/questions/873207/force-download-of-a-file-on-web-server-asp-net-c-sharp – Niels Apr 11 '13 at 16:17

2 Answers2

2

You can use a hidden IFRAME element and initiate a file download request, which does give the feeling for a AJAX file download.

While the file is being downloaded you can do other activity in your form on client side.

Yes you can call a webservice or aspx page or http handler as well in this URL

function dowloadFileJS()  {
      // Create an IFRAME.
      var iframe = document.createElement("iframe");

      // Point the IFRAME to GenerateFile
      iframe.src = "GenerateFile.aspx?yourQueryString=myQueryString";

      // This makes the IFRAME invisible to the user.
      iframe.style.display = "none";

      // Add the IFRAME to the page.  This will trigger a request to GenerateFile now.
      document.body.appendChild(iframe); 
    }
pim
  • 12,019
  • 6
  • 66
  • 69
Guru Kara
  • 6,272
  • 3
  • 39
  • 50
0

You can use Javascript to create an iframe to the file you want to download. Have a look at this answer: Starting file download with JavaScript

Community
  • 1
  • 1
Razvan Trifan
  • 534
  • 2
  • 6