简体   繁体   English

客户端从C#/ ASP.NET页中的Javascript调用开始下载文件吗?

[英]Start file download by client from Javascript call in C#/ASP.NET page?

I need to initiate file download from ASP.NET page from javascript code. 我需要从JavaScript代码启动从ASP.NET页面下载文件。

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? 是否可以调用Web服务来这样做,或者以某种方式调用C#页面的常规事件处理程序?

Note that I will retrieve a large amount of data, many megabytes. 请注意,我将检索大量数据,许多兆字节。

You can use a hidden IFRAME element and initiate a file download request, which does give the feeling for a AJAX file download. 您可以使用隐藏的IFRAME元素并启动文件下载请求,这确实给人以AJAX文件下载的感觉。

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 是的,您也可以在此URL中调用Web服务或aspx页面或http处理程序

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); 
    }

You can use Javascript to create an iframe to the file you want to download. 您可以使用Javascript为要下载的文件创建iframe。 Have a look at this answer: Starting file download with JavaScript 看看这个答案: 开始使用JavaScript下载文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM