2010-09-21 86 views
0

工作下面是代码下载文件没有在Chrome

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

它除了铬所有浏览器的工作。

我用框架也如下面的代码,但它在海量数据的情况下简化版,工作..

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) 
      } 
     } 
+0

你是否检查过弹出式窗口拦截器? – sheeks06 2010-09-21 06:24:48

+1

铬中发生了什么?如果窗口没有打开,那么可能是因为弹出窗口阻止程序。 – VinayC 2010-09-21 06:26:07

+0

你做了...代码在所有浏览器中工作,除了chorme。 – 2010-09-21 06:26:47

回答

2

下面是使用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; 
    }; 
} 

它适用于所有浏览器,并具有不弹出窗口的优势。

+0

感谢Mic为您的回复。但是这也造成了问题,当数据是非常大的序列化对象。在这种情况下,什么都不会发生。在我的情况下,数据量非常巨大。 – 2010-09-21 11:55:48

+1

通过GET请求发送的数据量(在url中)是有限的,取决于浏览器。如果它很大,你需要先用POST调用你的后端,接收一个id,然后用这个id进行GET调用。 – Mic 2010-09-21 12:18:23

+0

好的谢谢麦克...让我再检查一遍...... – 2010-09-21 13:21:55

0

尝试以下方法:

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.*/} 
} 

我认为它可能是下到moveTo()方法作为铬是一个纯粹的标签式浏览器。

也试试看看window.resizeTo(100,100)

+0

我曾试过这也罗伯特,但它没有为我工作。谢谢你的回应。 – 2010-09-21 13:24:03