lundi 2 juillet 2018

ASP.NET File download over HTTPS failure

Stuck on trying to download a file over https, works fine across http but get the message in firefox - 'FILENAME could not be saved because the source file could not be read' and 'Failed - Network Error' in Chrome.

Web application sits behind Cisco firewall in which i have opened all the ports and still no joy. Flomoxed.

    Dim file As System.IO.FileInfo = New System.IO.FileInfo(FullFilePath) '-- if the file exists on the server
    If file.Exists Then 'set appropriate headers

        Response.ClearContent()
        Response.ClearHeaders()

        Dim iStream As System.IO.Stream = Nothing
        ' Buffer to read 10K bytes in chunk:
        Dim buffer(10000) As Byte
        ' Length of the file:
        Dim length As Integer
        ' Total bytes to read:
        Dim dataToRead As Long

        Try
            ' Open the file.
            iStream = New System.IO.FileStream(FullFilePath, System.IO.FileMode.Open,
                                                   IO.FileAccess.Read, IO.FileShare.Read)

            ' Total bytes to read:
            dataToRead = iStream.Length

            'Response.ContentType = "application/octet-stream"
            Response.ContentType = "application/pdf"
            'Response.ContentType = WebApp.Common.Functions.ReturnContentType(file.Extension.ToLower())
            Response.AddHeader("Content-Disposition", "attachment; filename=""" & file.Name & """")

            ' Read the bytes.
            While dataToRead > 0
                ' Verify that the client is connected.
                If Response.IsClientConnected Then
                    ' Read the data in buffer
                    length = iStream.Read(buffer, 0, 10000)

                    ' Write the data to the current output stream.
                    Response.OutputStream.Write(buffer, 0, length)

                    ' Flush the data to the HTML output.
                    Response.Flush()

                    ReDim buffer(10000) ' Clear the buffer
                    dataToRead = dataToRead - length
                Else
                    'prevent infinite loop if user disconnects
                    dataToRead = -1
                End If
            End While

        Catch ex As Exception
            ' Trap the error, if any.
            Response.Write("Error : " & ex.Message)
        Finally
            If iStream IsNot Nothing Then
                ' Close the file.
                iStream.Close()
            End If
            Response.Close()
        End Try

        Response.End() 'if file does not exist
    Else
        Response.Write("This file does not exist.")
    End If 'nothing in the URL as HTTP GET




Aucun commentaire:

Enregistrer un commentaire