0

I'm trying to zip a set of pdf files and send to client as download.

No matter what combinations of Response settings I try, the code doesn't throw any exception and apparently the zip file stream is created fine, but the file is not sent to the client as a download and when you hit the download button nothing happens.

Private Sub lkbDownloadPdfs_Click(sender As Object, e As System.EventArgs) Handles aDownloadPdfs.ServerClick

    Try
        Dim WSStockToolAuthTokenUrl As String = ConfigurationManager.AppSettings("WSStockToolAuthTokenUrl")
        Dim auth As AuthenticationHeader = Utility.GetAuthenticationForStockToolToken()
        Dim token As String = Utility.GetInitializationToken(WSStockToolAuthTokenUrl, auth.UserName, auth.password)

        Response.Clear()
        Response.ContentType = "application/zip"
        Response.AppendHeader("Content-Disposition", "attachment; filename=files.zip")

        If (token IsNot Nothing) Then
            Dim result As String = PDFApiCallResult(token)
            Dim pdfPathList As List(Of String) = Utility.GeneratePDFList(result)
            If (pdfPathList.Count = 1) Then
                Dim pdfPath As String = pdfPathList.ElementAt(0)

                Dim strFile As String
                Dim strmZipOutputStream = New ZipOutputStream(Response.OutputStream)
                strmZipOutputStream.SetLevel(9)

                Dim objCrc32 As New Crc32()

                For Each strFile In pdfPathList
                    Dim Client As WebClient = New WebClient()
                    Dim strmFile As Stream = Client.OpenRead(strFile)

                    Dim reader As StreamReader = New StreamReader(strmFile)
                    Dim Content As String = reader.ReadToEnd()
                    Dim abyBuffer(Convert.ToInt32(Content.Length - 1)) As Byte

                    Dim sFile As String = Path.GetFileName(strFile)
                    Dim theEntry As ZipEntry = New ZipEntry(sFile)

                    theEntry.DateTime = DateTime.Now

                    theEntry.Size = Content.Length
                    strmFile.Close()
                    objCrc32.Reset()
                    objCrc32.Update(abyBuffer)
                    theEntry.Crc = objCrc32.Value
                    strmZipOutputStream.PutNextEntry(theEntry)
                    strmZipOutputStream.Write(abyBuffer, 0, abyBuffer.Length)
                Next

                strmZipOutputStream.Flush()
                strmZipOutputStream.Finish()
                strmZipOutputStream.Close()

                Response.Flush()
                Response.Close()
                Response.SuppressContent = True
                HttpContext.Current.ApplicationInstance.CompleteRequest()
            Else

            End If
        End If

    Catch ex As Exception
        ExceptionManager.Publish(ex)
    End Try

End Sub

Any help? (If you have working C# code I could try to convert it to vb.net too)

Update 1: This is the aspx where the link which does the callback resides:

<asp:UpdatePanel runat="server" ID="updImages" UpdateMode="Conditional"> ... DOWNLOAD PDFS ... <asp:AsyncPostBackTrigger ControlID="lkbAddToWishlist" /> <asp:AsyncPostBackTrigger ControlID="ddlCustomizations" /> </asp:UpdatePanel>

I've read that maybe the response is not working at all because of the ajax way the update panel does the postback, but not sure about that and how to deal with that.

Diego Perez
  • 2,188
  • 2
  • 30
  • 58
  • Have you tried the `Response.TransmitFile()` function as demonstrated [here](https://stackoverflow.com/questions/3252135/how-to-send-a-file-to-a-client-so-a-download-dialog-will-open/3253544)? – Hel O'Ween Nov 19 '21 at 13:26
  • Hi @Hel O'Ween, and sorry for my delay in the response. I tried transmit file and almost everything's possible. I've read that the problem could be related that the way the link does the postback (update panel and ajax) but no sure about that. Still wasn't able to stream the file to client directly without having to write it forst on disk and do a Response.Redirect. – Diego Perez Nov 24 '21 at 08:57

0 Answers0