无法下载word文档,抛出异常

时间:2009-12-21 13:27:07

标签: c# asp.net file download

我正在使用响应对象来下载作为内容存储在数据库中的word文档。抛出以下异常:

 SubStatusCode 'Response.SubStatusCode' threw an exception of type 'System.PlatformNotSupportedException'
 base {"This operation requires IIS integrated pipeline mode."} System.NotSupportedException  {System.PlatformNotSupportedException}
 Headers 'Response.Headers' threw an exception of type 'System.PlatformNotSupportedException'

我无法查看我的文件..我的代码如下:

protected void btnResumedload_Click(object sender, EventArgs e)
{
    DataTable dtResumeInfo = new DataTable();

    dtResumeInfo = bc.ConvertByteToDataTable(objservice.getResumeInfo(int.Parse(Session["LoginId"].ToString())));
    if (dtResumeInfo.Rows.Count > 0)
    {
        string doctype = dtResumeInfo.Rows[0]["ContentType"].ToString();
        string docname = dtResumeInfo.Rows[0]["FileName"].ToString();
        //
        try
        {
            Response.Buffer = false;
            Response.ClearHeaders();
            Response.ContentType = doctype;
            Response.AddHeader("Content-Disposition",
                     "attachment; filename=" + docname);
            //
            //Code for streaming the object while writing
            const int ChunkSize = 1024;
            byte[] buffer = new byte[ChunkSize];
            byte[] binary = (dtResumeInfo.Rows[0]["ContentData"]) as byte[];
            MemoryStream ms = new MemoryStream(binary);
            int SizeToWrite = ChunkSize;

            for (int i = 0; i < binary.GetUpperBound(0) - 1; i = i + ChunkSize)
            {
                if (!Response.IsClientConnected) return;
                if (i + ChunkSize >= binary.Length)
                    SizeToWrite = binary.Length - i;
                byte[] chunk = new byte[SizeToWrite];
                ms.Read(chunk, 0, SizeToWrite);
                Response.BinaryWrite(chunk);
                Response.Flush();
            }
            Response.Close();
        }
        catch (Exception ex)
        {
        lblmsg.Visible = true;
        lblmsg.Text = ex.Message;
        }
    }
    else
    {
        lblmsg.Visible = true;
        lblmsg.Text = "No Resume Information Found.";
    }
}

1 个答案:

答案 0 :(得分:0)

好像您使用的是Response.Headers属性,该属性仅受IIS 7.0集成管道模式支持。请参阅:IIS6 + HttpModule: This operation requires IIS integrated pipeline mode