0

the process:

  1. User opens web page, which contains an asp:LinkButton (download) and an asp:Button (next).
  2. "next" is disabled.
  3. User clicks "download". PDF file is generated in memory and issued as a download.

Need: enable "next"

Problem:

  1. can't download using javascript - must be server-side
  2. can't affect elements on web page using server-side without response.redirect
  3. can't issue a response.redirect because httpheaders have already been issued as part of step one.

I could go to a new page using "next" if it were enabled, but I want it disabled until the download is done.

Some relevant code:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If SessionHelper.PrintedEnvelope Or SessionHelper.PrintedTemplate Then
            Me.btnNext.Enabled = True
            Me.btnNext.CssClass = "nextButton"
        Else
            Me.btnNext.Enabled = False
        End If
    End Sub

  Protected Sub btnEnvelopeTemplate_Click(sender As Object, e As EventArgs) Handles btnEnvelopeTemplate.Click
        Try
            SessionHelper.PrintedTemplate = True

            Dim m As System.IO.MemoryStream = DeliveryHelper.generateEnvelope(True, "English")
            Dim data As Byte() = m.ToArray


            Response.ClearContent()
            Response.ClearHeaders()
            Response.ContentType = "application/download"
            Response.AppendHeader("Content-Length", data.Length.ToString())
            Response.AppendHeader("Content-Disposition", "attachment; filename=" + "EnvelopeTemplate.pdf")
            Response.AppendHeader("Accept-Ranges", "bytes")
            Response.OutputStream.Write(data, 0, data.Length)
            If Response.IsClientConnected Then
                Response.Flush()
                ' Response.End()
            End If

        Catch ex As Exception
            'stuff here
        End Try


    End Sub

I'm open to jquery ideas, too.

Magnus
  • 45,362
  • 8
  • 80
  • 118
user158017
  • 2,891
  • 30
  • 37
  • Take a look if this helps http://stackoverflow.com/questions/1106377/detect-when-browser-receives-file-download – Yuriy Galanter Oct 01 '13 at 21:59
  • it looks like it could - but I also found an easy work-around. If I open the pdf in browser instead of forcing a download, when they hit the back button to return to the screen, it auto-refreshes the page. Simpler and better for web-only screenreaders. But I'll look into this some more to see how would work just in case. – user158017 Oct 02 '13 at 17:24
  • found something better. see my answer. – user158017 Oct 02 '13 at 19:48

2 Answers2

0

I could go to a new page using "next" if it were enabled, but I want it disabled until the download is done

I'm sorry, this is simply not possible. There is no way to monitor the download progress and take action when it is complete, unless you write your own download handler mechanism. Browsers do not expose download progress to anything that can be accessed by script on your page.

John Wu
  • 50,556
  • 8
  • 44
  • 80
  • perhaps I was unclear. I really mean - once the person has clicked the link for the download. Even if there is an error. But the httpheaders have been written for the download and it won't don't it again. – user158017 Oct 02 '13 at 13:16
0

I found a solution that resolves the problem nicely!

  1. put the contents of the pdf in a hidden iframe on the page.
  2. disabled next button on the load.
  3. on the click of the linkbutton, I open the print dialog and re-enable the next button.

here is the button

 <asp:Button  
      ID="btnNext" 
      runat="server" 
      Text="Next" 
      ClientIDmode = "Static" 
      CssClass="" 
      Enabled="false"
      PostBackURL="[nextpage]"/>

Here are the links

<asp:LinkButton id="btnEnvelope" ClientIDMode="Static" runat="server" Text="Print your envelope" ToolTip="Click to Open Print Dialog" />

<asp:LinkButton id="btnEnvelopeTemplate" ClientIDMode="Static" runat="server" Text="Print this template" ToolTip="Click to Open Print Dialog" />

Here are the iframes

<iframe id="frameEnvelope" class="pdfIframe" src="Documents/Envelope.aspx"></iframe>
<iframe id="frameEnvelopeTemplate" class="pdfIframe" src="Documents/EnvelopeTemplate.aspx"></iframe>

Here is the Css

 .pdfIframe
 {
     display: none;
 }

Here is the javascript

function printFrame(id) {
         var frm = document.getElementById(id).contentWindow;
         frm.focus(); // focus on contentWindow is needed on some ie versions
         frm.print();
         document.getElementById('btnNext').disabled = false;
         return false;
     }

 $(document).ready(function () {
        //....other stuff

         $("#btnEnvelope").click(function () {
             printFrame("frameEnvelope");
         });

         $("#btnEnvelopeTemplate").click(function () {
             printFrame("frameEnvelopeTemplate");
         });
     });
user158017
  • 2,891
  • 30
  • 37