This is a migrated thread and some comments may be shown as answers.

Single Click Button with Streaming File

2 Answers 531 Views
Button
This is a migrated thread and some comments may be shown as answers.
Brian Goldman
Top achievements
Rank 2
Brian Goldman asked on 17 May 2011, 10:35 PM
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 Answers, 1 is accepted

Sort by
1
Accepted
Pero
Telerik team
answered on 19 May 2011, 08:44 AM
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.

Adam
Top achievements
Rank 1
commented on 06 Oct 2021, 09:14 AM

Vessy
Telerik team
commented on 06 Oct 2021, 01:57 PM

Thank you for sharing that, Adam!
0
Brian Goldman
Top achievements
Rank 2
answered on 19 May 2011, 11:41 PM
Worked Like a charm. Thank you.
Tags
Button
Asked by
Brian Goldman
Top achievements
Rank 2
Answers by
Pero
Telerik team
Brian Goldman
Top achievements
Rank 2
Share this question
or