Single Click Button with Streaming File

3 posts, 1 answers
  1. Brian Goldman
    Brian Goldman avatar
    34 posts
    Member since:
    Mar 2005

    Posted 17 May 2011 Link to this post

    Is it possible to stream a file when using the single click of rad button.
    For example I've setup a button exactly like the one in the Single Click Button demo but the button never gets enabled after the file is downloaded. I think it is because I'm using response.clear and response.end methods but I'm not sure. Here is the main part of the code in the button. Is there a workaround to this problem. The button is setup correctly because if I remove this code, it works properly.

    Dim downloadBytes As Byte() = pdfConverter.GetPdfFromUrlBytes(URL)
     
           Dim response As System.Web.HttpResponse = System.Web.HttpContext.Current.Response
           response.Clear()
           response.AddHeader("Content-Type", "binary/octet-stream")
           response.AddHeader("Content-Disposition", "attachment; filename=" + FileName + "; size=" + downloadBytes.Length.ToString())
           response.Flush()
           response.BinaryWrite(downloadBytes)
           response.Flush()
           response.End()

    Thank you,
    Brian
  2. Answer
    Pero
    Admin
    Pero avatar
    1156 posts

    Posted 19 May 2011 Link to this post

    Hi Brian,

    The button is not re-enabled because the response is cleared, and then modified to download a file. With a bit of JavaScript you could re-enable the button, after the user is prompted to download the file. I am using the approach described on the following page: http://geekswithblogs.net/GruffCode/archive/2010/10/28/detecting-the-file-download-dialog-in-the-browser.aspx.
    Here is an example:
    ASPX
    <%@ Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <script type="text/javascript">
            var fileDownloadCheckTimer;
            function OnClientClicked(sender, args)
            {
                var $ = $telerik.$;
     
                var token = new Date().getTime();
                $('#download_token_value_id').val(token);
     
                //disable button
                sender.set_enabled(false);
     
                fileDownloadCheckTimer = window.setInterval(function ()
                {
                    //get cookie and compare
                    var cookieValue = readCookie("fileDownloadToken");
                    if (cookieValue == token)
                    {
                        finishDownload();
                    }
                }, 1000);
            }
            function finishDownload()
            {
                window.clearInterval(fileDownloadCheckTimer);
                //clear cookie
                eraseCookie("fileDownloadToken");
                //enable button
                $find("RadButton1").set_enabled(true);
            }
     
            function createCookie(name, value, days)
            {
                if (days)
                {
                    var date = new Date();
                    date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
                    var expires = "; expires=" + date.toGMTString();
                }
                else var expires = "";
                document.cookie = name + "=" + value + expires + "; path=/";
            }
     
            function readCookie(name)
            {
                var nameEQ = name + "=";
                var ca = document.cookie.split(';');
                for (var i = 0; i < ca.length; i++)
                {
                    var c = ca[i];
                    while (c.charAt(0) == ' ') c = c.substring(1, c.length);
                    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
                }
                return null;
            }
     
            function eraseCookie(name)
            {
                createCookie(name, "", -1);
            }
        </script>
        <div>
            <asp:HiddenField ID="download_token_value_id" runat="server" />
            <telerik:RadButton ID="RadButton1" runat="server" Text="RadButton" OnClick="RadButton1_Click"
                UseSubmitBehavior="false" OnClientClicked="OnClientClicked">
            </telerik:RadButton>
        </div>
        </form>
    </body>
    </html>

    CS
    using System;
    using System.Web;
     
    public partial class Default_Button : System.Web.UI.Page
    {
        protected void RadButton1_Click(object sender, EventArgs e)
        {      
            int TestArraySize = 30;
            // create a dummy byte array
            byte[] bytearrayMessageContent = new byte[TestArraySize];
            for (int i = 0; i < TestArraySize; i++)
            {
                // fill character A-J into the dummy array
                bytearrayMessageContent[i] = (byte)(i % 10 + 65);
            }
     
            HttpResponse response = Response;
            response.Clear();
            response.AppendCookie(new HttpCookie("fileDownloadToken", download_token_value_id.Value));
            response.AddHeader("Content-Type", "binary/octet-stream");
            response.AddHeader("Content-Disposition", "attachment; filename=" + "FIle.txt" + "; size=" + TestArraySize.ToString());
            response.BinaryWrite(bytearrayMessageContent);
            response.Flush();
        }
    }

    I hope this helps.

    All the best,
    Pero
    the Telerik team

    Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.

  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Brian Goldman
    Brian Goldman avatar
    34 posts
    Member since:
    Mar 2005

    Posted 19 May 2011 Link to this post

    Worked Like a charm. Thank you.
Back to Top