以下是我應該這樣做:(/高速緩存磁盤/數據庫,您的通話)
- 創建生成的文件一個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;
}
}
}
正是我想要的和,像變魔術一樣 – rsapru 2009-02-05 15:37:04