我需要通过 javascript 代码从 ASP.NET 页面启动文件下载。
我最好的选择是什么?是否可以调用 Web 服务来执行此操作,或者以某种方式调用 C# 页面的常规事件处理程序?
请注意,我将检索大量数据,很多兆字节。
最佳答案
您可以使用隐藏的 IFRAME 元素并发起文件下载请求,这确实给人一种 AJAX 文件下载的感觉。
在下载文件时,您可以在客户端的表单中执行其他事件。
是的,您也可以在此 URL 中调用网络服务或 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);
}
关于javascript - 客户端从 C#/ASP.NET 页面中的 Javascript 调用开始文件下载?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15953840/