注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

我的博客

 
 
 

日志

 
 
关于我

风流茶说合,酒是色媒人,古往今来,浪涛依旧,我辈今朝,叱咤风云,

网易考拉推荐

用iframe实现异步下载  

2009-12-21 17:42:08|  分类: flex |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

ajax方式返回的数据有文本,xml和json这3种方式,显然是无法下载二进制格式的文件。项目需要异步下载的效果,只能求助于iframe了,本文简单的说明一下如何实现。

1、在页面添加一个隐藏的iframe:
<iframe id="iframereport" style="display:none;"></iframe>
2、添加js:
   function GetReportAnalysis(){
        try{
            $("iframereport").onreadystatechange=function(){AfterDownload();};
            $("iframereport").src="../PdfExportHandler.aspx?

oper=facksheetpdf&id="+$("hidManagerId").value;
        }catch(e){}
    }
    function AfterDownload(){
        try{
            var state=$("iframereport").contentWindow.document.readyState;
            if(state=="complete"){
                var msg=$("iframereport").contentWindow.document.body.innerHTML;
                if(msg)$alert(msg);
                else $alert("下载失败!");
            }
        }catch(e){}
    }
GetReportAnalysis方法用于下载按钮事件

3、给asp.net添加handle来处理请求:
/// <summary>
/// PdfExportHandler 的摘要说明
/// </summary>
public class PdfExportHandler: BaseUserHandler
{
    public override void Process()
    {
        string oper = StringUtils.ConvertToStr(Context.Request["oper"]);
        switch (oper.ToLower())
        {
            case "facksheetpdf":
                string id = StringUtils.ConvertToStr(Context.Request["id"]);
                GetFileAnalysis(id);
                break;
            default:
                break;
        }
    }
    #region Process Ajax
    private void GetFileAnalysis(string id)
    {
        try
        {
            string filePath = GetPdfPath(id);
            WritePdfFile(filePath, "基金经理");
        }
        catch (Exception ex)
        {
            this.Context.Response.Write("下载pdf时出现错误!");
            Configurations.Logger.Log(new WebMessage(Messages.Info_SysInfo_Error, "PdfExportHandler.GetReortAnalysis Error.", Context.Request.Url.PathAndQuery, false, ex));
        }
    }
    private void WritePdfFile(string filePath, string fileName)
    {
        if (File.Exists(filePath))
        {
            FileStream fStream = new FileStream(filePath, FileMode.Open);
            try
            {
                Context.Response.Clear();
                Context.Response.ContentType = "application/pdf";
                Context.Response.AppendHeader("Content-Disposition", "attachment;filename=" + Context.Server.UrlEncode(fileName) + ".pdf;charset=GB2312");

                long fileLength = fStream.Length;
                int size = 10240;//10K一--分块下载,10K为1块
                byte[] readData = new byte[size];
                if (size > fileLength)
                    size = Convert.ToInt32(fileLength);
                long fPos = 0;
                bool isEnd = false;
                while (!isEnd)
                {
                    if ((fPos + size) >= fileLength)
                    {
                        size = Convert.ToInt32(fileLength - fPos);
                        isEnd = true;
                    }
                    readData = new byte[size];
                    fStream.Position = fPos;
                    fStream.Read(readData, 0, size);
                    this.Context.Response.BinaryWrite(readData);
                    this.Context.Response.OutputStream.Flush();
                    fPos += size;
                }
            }
            finally
            {
                fStream.Close();
                this.Context.Response.End();
            }
        }
    }
    public string GetPdfPath(string id)
    {
        string today = DateTime.Today.ToString("yyyyMMdd"),
        yesterday = DateTime.Today.AddDays(-1).ToString("yyyyMMdd");

        string htmlFileFormat = "FactSheet-Manager-{0}.pdf";
        string htmlFileFormatPath = Path.Combine(Configurations.FactSheetHtmlPath, today);
        htmlFileFormatPath = Path.Combine(htmlFileFormatPath, string.Format(htmlFileFormat, id));
        if (!File.Exists(htmlFileFormatPath))
        {
            htmlFileFormatPath = Path.Combine(Configurations.FactSheetHtmlPath, yesterday);
            htmlFileFormatPath = Path.Combine(htmlFileFormatPath, string.Format(htmlFileFormat, id));
        }
        return htmlFileFormatPath;
    }
    #endregion
}

web.config注册handle:
<add path="PdfExportHandler.aspx" verb="*" type="PdfExportHandler"/>

  评论这张
 
阅读(234)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017