2017-06-12 168 views
0

我有一个包含许多PDF的文件夹。当用户从DropDownList中选择类别并单击按钮时,系统将文件(不是全部)复制到新文件夹中,然后生成压缩文件。AsyncCallback功能不起作用

这些步骤是可以的,但问题是生成的zip服务完成时。

我打算写一个例子:

<asp:Content ID="Content2" ContentPlaceHolderID="body" runat="Server"> 

    <asp:ScriptManagerProxy ID="ScriptManagerProxy1" runat="server"> 
     <Services> 
      <asp:ServiceReference Path="SomeService.asmx" InlineScript="true" /> 
     </Services> 
    </asp:ScriptManagerProxy> 

    <script type="text/javascript"> 
     function copyFiles(id) { 
      SomeService.CopyFiles($ja("#DropDownList1").val(), id, FuncionSucceeded, FuncionFailed); 
     } 

     function FuncionSucceeded(result, eventArgs) { 
      document.getElementById("GenerateZip").click(); 
     } 

     function FuncionFailed(result, eventArgs) { 

     } 


    </script> 

    <asp:UpdatePanel ID="UpdatePanel1" runat="server"> 
     <ContentTemplate> 
      <asp:Button ID="Button1" runat="server" Text="Generate Zip" /> 
      <asp:Timer ID="Timer1" runat="server" Interval="5000" Enabled="false"> 
      </asp:Timer> 
      <asp:Button ID="btnGenerateZip" runat="server" Style="display: none;" ClientIDMode="Static" /> 
      <asp:Image ID="Image1" runat="server" ImageUrl="~/images/loading.gif" 
       Visible="False" /> 
      <asp:Label ID="Label1" runat="server" Text="" Visible="false"></asp:Label> 
      <asp:LinkButton runat="server" Visible="false" ID="linkzip" Text="Descargar" /> 
     </ContentTemplate> 
     <Triggers> 
      <asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" /> 
     </Triggers> 
    </asp:UpdatePanel> 
</asp:Content> 

,代码:

'This code call the client side to copy the files 
Protected Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click 
    If DropDownList1.SelectedValue = "0" Then 
     Exit Sub 
    End If 

    Dim pathName As String = "The path here" 

    If Not File.Exists(pathName) Then 
     Dim folder As String = Path.GetDirectoryName(pathName) 
     If Not Directory.Exists(folder) Then 
      Directory.CreateDirectory(folder) 
     End If 
    End If 

    Image1.Visible = True 
    Label1.Visible = True 
    Timer1.Enabled = True 
    Label1.Text = "Generating zip" 

    Dim script As String = "copyFiles('1234');" 
    ScriptManager.RegisterClientScriptBlock(Me.Page, Me.Page.GetType(), Guid.NewGuid().ToString(), script, True) 

End Sub 

'When the client side returns the function `FunctionSucceeded` call next function 

Private Sub btnGenerateZip_Click(sender As Object, e As EventArgs) Handles btnGenerarZip.Click 
    GenerateZip() 
End Sub 

Private Sub GenerateZip() 
    Try 
     Dim ws As New AnotherService.ServiceExample 
     Dim ar2 As New AsyncCallback(AddressOf DownloadFile) 
     Dim pathName As String = String.Format("{0}", path) 

     result = ws.BeginSomeFunction(path, ar2, ws) 

    Catch ex As Exception 
     generaZip = False 
    End Try 

End Sub 

而且回调函数:

Protected Sub DownloadFile(ar As IAsyncResult) 
     Dim ws As New AnotherService.ServiceExample 
     Try 
      Dim pathName As String ="Path" 
      ws.deleteFolder(pathName) 
      ws.EndSomeFunction(ar) 
      Image1.Visible = False 
      Label1.Text = "OK" 
      Timer1.Enabled = Image1.Visible 
      linkzip.Visible = True 
      generaZip = True 
     Catch ex As Exception 
      generaZip = False 
     End Try 
    End Sub 

和计时器滴答:

Protected Sub Timer1_Tick(sender As Object, e As System.EventArgs) Handles Timer1.Tick 
     Select Case generaZip 
      Case True 
       Image1.Visible = False 
       Label1.Text = "Everything is ok" 
       Timer1.Enabled = Image1.Visible 
       linkzip.Visible = True 
      Case False 
       Image1.Visible = False 
       Label1.Text = "Error" 
       Timer1.Enabled = Image1.Visible 
     End Select 

    End Sub 

问题是当我们在DownloadFile函数(异步回调函数),我在那里没有任何工作。我改变了许多属性,值,变量值,当tick函数触发时,就像我没有做任何事情。

我已经改变了所有这些行:

Image1.Visible = False 
Label1.Text = "OK." 
Timer1.Enabled = Image1.Visible 
linkzip.Visible = True 
generaZip = True 
+0

你的意思是generaZip仍然是假的?这是在哪里宣布的,我们确定它获得/保留它的价值? –

+0

yes仍然是false,image1.visible仍然是true,以及所有其他属性。 @JimmySmith 在页面顶部声明并且在不是回发时初始化 – Ary

+0

我不确定它是否会以这种方式正确保留此True值。如果你创建一个会话变量并将True分配给它,会发生什么? 'Select Case Session(“generaZip)' –

回答

0

我声明如下共享generaZip变量,并以这种方式它的正常工作。 谢谢全部

+0

很高兴你知道了,我从来没有注意到你的代码里声明了这个变量。 –