【问题标题】:How to download word document stored in database as content data in sql server?如何下载存储在数据库中的word文档作为sql server中的内容数据?
【发布时间】:2011-01-01 05:36:51
【问题描述】:

如何通过C#下载存储在数据库、sql server中的word文档作为内容数据?

这是我正在使用的代码:

string doctype = dtResumeInfo.Rows[0]["ContentType"].ToString();
            string docname = dtResumeInfo.Rows[0]["FileName"].ToString();
            //
            try
            {
                Response.Buffer = false;
                Response.ClearContent();
                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)
            {

            }

但是,它显示以下错误:

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'

我发布的 IIS 版本是 6.0。只能在 IIS 7 中下载 word 文档。所以,这就是错误的原因。有没有其他方法可以克服这个问题?任何其他代码将支持较低版本?请帮帮我。 (对不起,长消息..)

【问题讨论】:

    标签: c# asp.net sql-server file-upload


    【解决方案1】:

    问题是我在母版页中使用更新面板。UpdatePanel 控件使用异步回发来控制页面的哪些部分被渲染。所以,我在 aspx 页面中的按钮中添加了以下代码。

    <asp:UpdatePanel ID="UpdatePanel2" runat="server">
                        <ContentTemplate>
                            <asp:Button ID="btnResumedload" Text="Download Resume" runat="server" BackColor="Maroon"
                                ForeColor="White" Font-Bold="true" OnClick="btnResumedload_Click" Height="27px"
                                Width="195px" />
    
                        </ContentTemplate>
                         <Triggers>
                                <asp:PostBackTrigger ControlID="btnResumedload" />
                            </Triggers>
                    </asp:UpdatePanel>
    

    它现在正在工作..非常感谢你:-)

    【讨论】:

      【解决方案2】:

      我假设您发布的代码来自通用处理程序。 这种尝试应该在 IIS 6 下工作。

      ...
      byte[] data = null;
      // fill data
      context.Response.ContentType = doctype;
      context.Response.OutputStream.Write(data, 0, data.Length);
      

      【讨论】:

      • 它是否适用于除 word 文件之外的其他文档?如果不是,那么您的通用处理程序的实现可能是错误的
      猜你喜欢
      • 2017-03-25
      • 1970-01-01
      • 1970-01-01
      • 2012-01-29
      • 2010-12-08
      • 2011-07-18
      • 2016-11-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多