Imports System.Net
Imports System.IO

Namespace PCSolution

    Public Class File

        Inherits System.Web.UI.Page

#Region "  ULOZIT SOUBOR Z INTERNETU NA DISK  "

        ''' <summary>
        ''' Uložit soubor na disk, vždy se prohlížeč zeptá jestli soubor uložit, i když je 
        ''' v nastavení, že se má soubor otevřít rovnou v okně prohlížeče
        ''' </summary>
        ''' <param name="sFile">Název souboru ve formatu /Foto/Soubor.jpg</param>
        ''' 
        Sub SaveFileFromInternetToDisk(ByVal sFile As String)

            Dim strPhysicalPath As String
            Dim objFileInfo As System.IO.FileInfo
            Try
                strPhysicalPath = Server.MapPath(sFile)

                If Not System.IO.File.Exists(strPhysicalPath) Then
                    HttpContext.Current.Response.Write(strPhysicalPath)
                    Exit Sub
                End If

                objFileInfo = New System.IO.FileInfo(strPhysicalPath)
                HttpContext.Current.Response.Clear()

                HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" & _
                    objFileInfo.Name)
                HttpContext.Current.Response.AddHeader("Content-Length", objFileInfo.Length.ToString())

                HttpContext.Current.Response.ContentType = "application/octet-stream"
                HttpContext.Current.Response.WriteFile(objFileInfo.FullName)

            Catch

            Finally

                HttpContext.Current.Response.End()

            End Try

        End Sub



#End Region

#Region "  ULOZIT SOUBORY Z INTERNETU DO PROMENNE  "

        ''' <summary>
        ''' Uloží požadovanou externí stránku do proměnné
        ''' </summary>
        ''' <param name="url">WWW adresa</param>
        ''' <returns>Řetězec s obsahem stránky</returns>
        ''' <remarks></remarks>
        Function SaveExternalHTMLPageToString(ByVal url As String) As String

            Dim ret As String = ""

            Dim objWebClient As New WebClient()
            Dim objUTF8 As New UTF8Encoding()
            Try
                ret = objUTF8.GetString(objWebClient.DownloadData(url))
                Return ret
            Catch
                Return ""
            End Try


        End Function

        ''' <summary>
        ''' Uloží požadovaný externí soubor do proměnné
        ''' </summary>
        ''' <param name="url">WWW adresa k souboru</param>
        ''' <param name="SaveFile">Do ktereho souboru se ma soubor z webu ulozit</param>
        ''' <remarks></remarks>
        Sub SaveExternalFileToString(ByVal url As String, ByVal SaveFile As String)

            Dim ret As String = ""

            Dim objWebClient As New WebClient()
            'Dim objUTF8 As New UTF7Encoding()
            Dim FS As FileStream
            Dim SW As BinaryWriter

            FS = New FileStream(SaveFile, FileMode.Create)
            SW = New BinaryWriter(FS)
            SW.Write(objWebClient.DownloadData(url))
            SW.Close() : FS.Close()

        End Sub

        ''' <summary>
        ''' Uloží požadovanou externí stránku zabezpečenou heslem do proměnné
        ''' </summary>
        ''' <param name="url">WWW adresa</param>
        ''' <returns>Řetězec s obsahem stránky</returns>
        ''' <remarks></remarks>
        Function SaveExternalHTMLPageWithPasswordToString(ByVal url As String, ByVal username As String, ByVal password As String, ByVal domain As String) As String

            Dim ret As String = ""

            Dim objWebClient As New WebClient()
            Dim objUTF8 As New UTF8Encoding()
            objWebClient.Credentials = New System.Net.NetworkCredential(username, password, domain)

            Try
                ret = objUTF8.GetString(objWebClient.DownloadData(url))
                Return ret
            Catch ex As Exception
                Return ex.ToString
                Return ""
            End Try


        End Function

#End Region

#Region "  ZOBRAZIT VELIKOST SOUBORU  "

        ''' <summary>
        ''' Uzivatelsky privetiva velikost souboru, prevede velikost z Bytu na velikost nejblizsi uzivateli.
        ''' </summary>
        ''' <param name="iSize">Velikost v bytech</param>
        ''' <returns>Napr.: 14kB, 1,5MB, ...</returns>
        ''' <remarks></remarks>
        Function GetFileSizeUserFriendly(ByVal iSize As Integer) As String

            If iSize < 1024 Then
                Return iSize & " B"
            ElseIf iSize < 1048576 Then
                Return Math.Round(iSize / 1024, 2) & " kB"
            Else
                Return Math.Round(iSize / 1024 / 1024, 2) & " MB"
            End If

        End Function

#End Region

    End Class

End Namespace
