0

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

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

1 回答 1

0

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

于 2013-07-05T07:45:19.130 回答