[英]Download file in javascript is not working in Chrome
below is the code 下面是代码
function ExportToExcel() {
if ($("#dateRange").val() != "") {
var frm = $("#frmProjectReport").serialize();
var url = "/Reports/ProjectExcelReport?" + frm;
Download(url);
}
}
function Download(url) {
alert(url);
//var win = window.open(url, "DownloadWin", "resizable=0,status=0,toolbar=0,width=600px,height=300px");
var win = window.open(url, "DownloadWin", "width=600px,height=300px,scrollbars=yes ,menubar=no,location=no,left=0,top=0")
win.focus();
win.moveTo(100, 100);
}
its working in all browser except chrome. 它可以在除chrome之外的所有浏览器中使用。
I have used frame also as below code but it does't work in case of huge data.. 我也使用以下代码作为框架,但是在海量数据的情况下它不起作用。
function Download(url) {
try {
$("#fileIframe").html("");
var iframe = $('<iframe name="postframe" id="postframe" class="hidden" frameBorder="0" src="about:none" />');
$('#fileIframe').append(iframe);
$('#frmProjectReport').attr("action", url);
$('#frmProjectReport').attr("method", "post")
$('#frmProjectReport').attr("target", "postframe")
$('#frmProjectReport').submit();
//win = window.open(url, "DownloadWin", "width=600px,height=300px,scrollbars=yes ,menubar=no,location=no,left=0,top=0")
//win.focus();
//win.moveTo(100, 100);
}
catch (e) {
alert(e)
}
}
Here is the way we export an excel file using an IFRAME
: 这是我们使用
IFRAME
导出Excel文件的方式:
function download(src){
var ifr = document.createElement('iframe');
ifr.style.display = 'none';
document.body.appendChild(ifr);
ifr.src = src;
ifr.onload = function(e){
document.body.removeChild(ifr);
ifr = null;
};
}
It works in all browsers, and has the advantage of not popping up a window. 它适用于所有浏览器,并且具有不弹出窗口的优点。
Try the following: 请尝试以下操作:
function Download(url){
try
{
var win = window.open(url,"DownloadWin","width=600px,height=300px,scrollbars=yes ,menubar=no,location=no,left=0,top=0");
try
{
win.focus();
win.moveTo(100, 100);
}catch(e){/*Focus|moveTo not supported*/}
}catch(e){/*open not supported, doubt it.*/}
}
I think it may be down to the moveTo() method as Chrome is a pure tabbed browser. 我认为它可能归结于moveTo()方法,因为Chrome是纯标签式浏览器。
Also try check out the window.resizeTo(100,100)
也尝试检查
window.resizeTo(100,100)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.