簡體   English   中英

如何在SQL Server中下載存儲在數據庫中的Word文檔作為內容數據?

[英]How to download word document stored in database as content data in sql server?

如何通過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文檔的下載。因此,這是導致錯誤的原因。 還有其他方法可以解決此問題嗎? 任何其他代碼將支持較低版本? 請幫幫我。 (很抱歉,長消息。)

問題是我在母版頁中使用更新面板。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>

它現在正在工作..非常感謝你:-)

我假設您發布的代碼來自通用處理程序。 此嘗試應在IIS 6下工作。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM