1

我需要从 ASP.NET 页面从 javascript 代码启动文件下载。

我最好的选择是什么?是否可以调用 Web 服务来执行此操作,或者以某种方式调用 C# 页面的常用事件处理程序?

请注意,我将检索大量数据,数兆字节。

4

2 回答 2

2

您可以使用隐藏的 IFRAME 元素并发起文件下载请求,这确实给人一种 AJAX 文件下载的感觉。

下载文件时,您可以在客户端的表单中执行其他活动。

是的,您也可以在此 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); 
    }
于 2013-04-12T01:55:09.663 回答
0

您可以使用 Javascript 为要下载的文件创建 iframe。看看这个答案:使用 JavaScript 开始文件下载

于 2013-04-11T20:16:12.380 回答