تقييم الموضوع :
  • 0 أصوات - بمعدل 0
  • 1
  • 2
  • 3
  • 4
  • 5
طلب كتابة كلاس تحميل ملف
#8
كود :
' This class helps and supplies the methods to
' 1. Initialize a HttpWebRequest object.
' 2. Check the url and initialize some properties of a downloader.
' 3. Check whether the destination file exists. If not, create a file with
'    the same size as the file to be downloaded.

Imports System.Net
Imports System.Text.RegularExpressions
Imports System.IO

Public NotInheritable Class FileDownloaderUtility

    Private Sub New()
    End Sub

    Public Shared Function InitializeHttpWebRequest(ByVal downloader As IFileDownloader) As HttpWebRequest
        Dim request = CType(WebRequest.Create(downloader.Url), HttpWebRequest)

        If downloader.Credentials IsNot Nothing Then
            request.Credentials = downloader.Credentials
        Else
            request.Credentials = CredentialCache.DefaultCredentials
        End If

        If downloader.Proxy IsNot Nothing Then
            request.Proxy = downloader.Proxy
        Else
            request.Proxy = WebRequest.DefaultWebProxy
        End If

        Return request
    End Function

    ''' <summary>
    ''' Check the URL to download, including whether it supports Range,
    ''' </summary>
    ''' <param name="downloader"></param>
    ''' <returns></returns>
    Public Shared Function CheckUrl(ByVal downloader As IFileDownloader) As String
        Dim fileName As String = String.Empty

        ' Check the file information on the remote server.
        Dim webRequest = InitializeHttpWebRequest(downloader)

        Using response = webRequest.GetResponse()
            For Each header In response.Headers.AllKeys
                If header.Equals("Accept-Ranges", StringComparison.OrdinalIgnoreCase) Then
                    downloader.IsRangeSupported = True
                End If

                If header.Equals("Content-Disposition", StringComparison.OrdinalIgnoreCase) Then
                    Dim contentDisposition As String = response.Headers(header)

                    Dim pattern As String = ".[^;]*;\s+filename=""(?<file>.*)"""
                    Dim r As New Regex(pattern)
                    Dim m As Match = r.Match(contentDisposition)
                    If m.Success Then
                        fileName = m.Groups("file").Value
                    End If
                End If
            Next header

            downloader.TotalSize = response.ContentLength

            If downloader.TotalSize <= 0 Then
                Throw New ApplicationException("The file to download does not exist!")
            End If

            If Not downloader.IsRangeSupported Then
                downloader.StartPoint = 0
                downloader.EndPoint = Integer.MaxValue
            End If
        End Using

        If downloader.IsRangeSupported AndAlso (downloader.StartPoint <> 0 _
                                                OrElse downloader.EndPoint <> Long.MaxValue) Then
            webRequest = InitializeHttpWebRequest(downloader)

            If downloader.EndPoint <> Integer.MaxValue Then
                webRequest.AddRange(downloader.StartPoint, downloader.EndPoint)
            Else
                webRequest.AddRange(downloader.StartPoint)
            End If
            Using response = webRequest.GetResponse()
                downloader.TotalSize = response.ContentLength
            End Using
        End If

        Return fileName
    End Function


    ''' <summary>
    ''' Check whether the destination file exists. If not, create a file with the same
    ''' size as the file to be downloaded.
    ''' </summary>
    Public Shared Sub CheckFileOrCreateFile(ByVal downloader As IFileDownloader, ByVal fileLocker As Object)
        ' Lock other threads or processes to prevent from creating the file.
        SyncLock fileLocker
            Dim fileToDownload As New FileInfo(downloader.DownloadPath)
            If fileToDownload.Exists Then

                ' The destination file should have the same size as the file to be downloaded.
                If fileToDownload.Length <> downloader.TotalSize Then
                    Throw New ApplicationException(
                        "The download path already has a file which does not match" _
                        & " the file to download. ")
                End If

                ' Create a file.
            Else
                If downloader.TotalSize = 0 Then
                    Throw New ApplicationException("The file to download does not exist!")
                End If

                Using fileStream As FileStream = File.Create(downloader.DownloadPath)
                    Dim createdSize As Long = 0
                    Dim buffer(4095) As Byte
                    Do While createdSize < downloader.TotalSize
                        Dim bufferSize As Integer = If((downloader.TotalSize - createdSize) < 4096, CInt(Fix(downloader.TotalSize - createdSize)), 4096)
                        fileStream.Write(buffer, 0, bufferSize)
                        createdSize += bufferSize
                    Loop
                End Using
            End If
        End SyncLock
    End Sub

End Class
الرد }}}
تم الشكر بواسطة: سعود , kslawy


الردود في هذا الموضوع
طلب كتابة كلاس تحميل ملف - بواسطة ali.alfoly - 02-04-14, 05:33 AM
RE: طلب كتابة كلاس تحميل ملف - بواسطة silverlight - 03-04-14, 04:37 PM
RE: طلب كتابة كلاس تحميل ملف - بواسطة kslawy - 05-04-14, 12:42 AM

المواضيع المحتمل أن تكون متشابهة .
الموضوع : الكاتب الردود : المشاهدات : آخر رد
  تحميل قائمة فيديوهات كاملة من اليوتيوب dr.programming 1 161 06-12-25, 02:26 AM
آخر رد: princelovelorn
  كود تحميل فيديو من اليوتيوب عبر بايثون justforit 4 265 23-10-25, 02:58 AM
آخر رد: justforit
  [VB.NET] احتاج مساعدة في كتابة كود sloom00 2 955 21-08-25, 09:09 AM
آخر رد: sloom00
  مساعدة في طريقة تحميل الفيديوهات المخزنة في قاعدة البيانات foad8920 0 660 24-04-25, 12:58 PM
آخر رد: foad8920
Photo رابط تحميل برنامج منبه الصلاة | Prayer Alarm mmaalmesry 0 740 24-10-24, 05:49 PM
آخر رد: mmaalmesry
  حل مشكلة تعذر تحميل الملف أو التجميع ل scannerlib ahlamalgomate2020 0 299 01-09-24, 03:05 PM
آخر رد: ahlamalgomate2020
  [VB.NET] كتابة تلقائية علاء الكبابي 2 368 17-08-24, 03:07 AM
آخر رد: علاء الكبابي
  يوجد لدي بطئ في تحميل الصور داخل FlowLayoutPanel1 يوجد لدي اكثر من 100 صنف mohamedgadain61 1 452 02-06-24, 12:44 AM
آخر رد: غزة العزة
  [VB.NET] تحديث بيانات فى كلاس من جدول سيكوال asdfar1977 6 714 26-02-24, 01:07 AM
آخر رد: asdfar1977
  تحميل جميع الماسحات الضوئية المتصلة بالجهاز في كومبوبوكس جيولوجي مبتدئ 2 581 22-02-24, 07:10 PM
آخر رد: جيولوجي مبتدئ

التنقل السريع :


يقوم بقرائة الموضوع: