views:

116

answers:

2

How to download a word document which is stored in database, sql server as a content data through C#?

This is my Code I'm using:

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)
            {

            }

But, it is showing the following error:

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'

My IIS version is 6.0 where I published. Donwloading word document can be done only in IIS 7. So, this is the cause of the error. Is there any other way to overcome this problem? Any other code will support lower version?? Plz, help me. (Sorry for long message.. )

A: 

I'm assuming the code you posted is from a generic handler. This attempt should work under IIS 6.

...
byte[] data = null;
// fill data
context.Response.ContentType = doctype;
context.Response.OutputStream.Write(data, 0, data.Length);
citronas
Does it work with other documents than word files? If not, you're implementation of the generic handler might be wrong
citronas
A: 

Hi!

Problem is I'm using update panel in master page.. The UpdatePanel control uses asynchronous postbacks to control which parts of the page get rendered.So, I added following code to my button in aspx page.

<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>

It is working now.. Thank u very much :-)

Nila