下载在IE8中无效的Word / Doc文件代码

时间:2013-07-05 07:21:58

标签: c# asp.net internet-explorer-8

这里我使用网格点击的行命令事件下载一个word / doc文件,我写了一个方法downloadresume。这个方法在所有浏览器中工作正常,除了Internet浏览器在IE中生成.aspx而不是file.can任何一个帮我解决这个问题

protected void grdCandidate_RowCommand(object sender, GridViewCommandEventArgs e)
{
    try
    {
        if (e.CommandName == "Download")
        {
            byte[] Attachment = null;
            string Extension = string.Empty;
            ClsCandidateManager objCandidateManager = new ClsCandidateManager();
            Attachment = objCandidateManager.GetCandidateAttachment(Convert.ToInt32(e.CommandArgument), out Extension);
            DownloadAttachment("Resume", Attachment, Extension);
        }
    }
    catch (Exception ex)
    {
        string str = ex.Message + ex.InnerException;

    }
}

public void DownloadAttachment(string strFileName, byte[] Attachment, string Extension)
{
    if (Attachment != null && Attachment.Length > 0)
    {
        Page.Response.Clear();
        Page.Response.Buffer = true;
        Page.Response.Charset = "";
        if (Extension == ".pdf")
        {
            Page.Response.ContentType = "application/pdf";
        }
        else
        {
            Page.Response.ContentType = "application/vsd-msword";
        }
        Page.Response.AddHeader("content-disposition", "attachment;filename=" + strFileName + Extension);

        Page.Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Page.Response.BinaryWrite(Attachment);
        Page.Response.Flush();
        //Response.End();
    }
    else
    {
        ScriptManager.RegisterStartupScript(this, GetType(), "Attachment", "alert('Attachment not found!');", true);
    }
}

1 个答案:

答案 0 :(得分:0)

您需要在“Internet选项”的“安全”选项卡中启用“文件下载”选项,以从浏览器下载文件。 检查从IE下载Word / Doc文件的链接 - http://ais-ss.usc.edu/helpdoc/main/browser/bris004b.html