2009-02-04 41 views
1

生成的I有一個要求,其中i需要顯示一個等待消息在執行一個excel報告,並且當執行完成時,Excel被寫入到響應於用戶保存。一旦保存完成,我需要隱藏等待消息。等待頁而Excel是在ASP.net

我能夠證明等待消息,但我不能夠隱藏相同的執行完成時。我已經使用Ifames來實現相同。 我在iframe中執行excel所需的所有處理,並在相同的Iframe中寫入響應。我試圖用Javascript隱藏等待消息,但是沒有執行在onload,onunload,onbeforeunload等iframe中編寫的任何JavaScript函數。

有什麼辦法,我可以調用JavaScript函數,或有任何其他的方法解決問題。

回答

4

以下是我應該這樣做:(/高速緩存磁盤/數據庫,您的通話)

  • 創建生成的文件一個HttpHandler,將其保存並返回一個標識符生成的文檔
  • 調用HttpHandler的使用AJAX調用(使用jQuery使這個很容易)
  • 使用JavaScript/jQuery來顯示等待消息/圖形/動畫/不管
  • 在AJAX請求回調,重定向到一個pa ge將採用生成的標識符並使​​用Content-Disposition標題作爲附件發送相應的文檔。

作爲一個側面說明,這將是在ASP.NET MVC

GenerateDocument HttpHandler的存根一點更直截了當:

public class GenerateMyDocumentHandler : IHttpHandler 
{ 
    #region [ IHttpHandler Members ] 

    public Boolean IsReusable 
    { 
     get { return true; } 
    } 

    public void ProcessRequest(HttpContext context) 
    { 
     var docIdentifier = this.GenerateDocument(); 

     context.Response.ContentType = "text/plain"; 
     context.Response.Write(docIdentifier.ToString("N")); 
    } 

    #endregion 

    private Guid GenerateDocument() 
    { 
     var identifier = Guid.NewGuid(); 

     // Logic that generates your document and saves it using the identifier 

     return identifier; 
    } 
} 

客戶端腳本存根:

function generateDocument() { 
    var result = $.get('/GenerateDocument.ashx', { any: 'parameters', you: 'need', go: 'here' }, generateDocumentCallback, 'text'); 
    // display your waiting graphic here 
} 

function generateDocumentCallback(result) { 
    window.location.href = '/RetrieveDocument.ashx/' + result; 
} 

RetrieveDocument HttpHandler的存根:

public class RetrieveDocument : IHttpHandler 
{ 
    public void ProcessRequest(HttpContext context) 
    { 
     var identifier = new Guid(context.Request.Url.Segments[1]); 
     var fileContent = this.GetFileAsBytes(identifier); 

     context.Response.ContentType = "application/vnd.ms-excel"; 
     context.Response.AddHeader("Content-Disposition", "attachment; filename=yourfilename.xls"); 
     context.Response.OutputStream.Write(fileContent, 0, fileContent.Length); 
    } 

    private Byte[] GetFileAsBytes(Guid identifier) 
    { 
     Byte[] fileBytes; 
     // retrieve the specified file from disk/memory/database/etc 

     return fileBytes; 
    } 

    public Boolean IsReusable 
    { 
     get 
     { 
      return true; 
     } 
    } 
} 
+0

正是我想要的和,像變魔術一樣 – rsapru 2009-02-05 15:37:04

2

我幾年前做了這樣的事情。我打開了一個新線程來執行後臺處理,然後重定向到「等待」頁面。等待頁面檢查任務的狀態,如果完成,則重定向回去。這是一個真正簡單的解決方案。檢查出來here

+0

+1有一個良好的閱讀 – 2009-02-04 17:01:55