Results 1 to 1 of 1

Thread: Memory Stream To File Stream and Write File With progress Loop

  1. #1

    Thread Starter
    PowerPoster make me rain's Avatar
    Join Date
    Sep 2008
    Location
    india/Hubli
    Posts
    2,205

    Memory Stream To File Stream and Write File With progress Loop

    I am trying to Report the Write progress of a file in the background worker from Memory stream to file stream
    Of course the file is writing to local system, but without the progress display

    I tried to use the WebClient.DownLoadFile method which provides the progress event, but which is buggy and demands elevated UAC of the local file system, and some/many times the file size returns will be 0 bytes, etc...

    How to do it please

    vb.net Code:
    1. ''' <summary>
    2.     ''' Down load file from FTP site With progress & Write the Downloaded Memory Steram To File Stream
    3.     '''
    4.     ''' <Status>BUG</Status>
    5.     '''
    6.     ''' <issues>
    7.     ''' Unable to track Byte Write From Memory Stream To File stream
    8.     ''' Since Method  BytesRed = DownLoadedMomoryStream.Read(WritingByteArray, 0, 1048576)
    9.     ''' Reads all bytes to BytesRed at once, but not in a Progressive Sequeence
    10.     ''' Therefore Unable to Display the profress Bar Progress
    11.     ''' </issues>>
    12.     '''
    13.     ''' </summary>
    14.     Private Sub DownLoadFile()
    15.  
    16.         'Down load destination On local file system
    17.         Dim downloaddestination As String = $"{My.Settings.DownLoadPath}/{_DestinationFileName}"
    18.         downloaddestination = downloaddestination.Replace("/", "")
    19.  
    20.         'Initialize FtpManager a Local class, Which can able to return the own loaded memory stream
    21.         Dim _FileToDownLoadFromServer As String = "/Img/222.jpg"
    22.         Dim f As New FtpManager(_ACCID, "remoteclient", "26101982", downloadingfilepath:=_FileToDownLoadFromServer)
    23.  
    24.         ' Get the Memory Stream Of Server File From f Class
    25.         Dim DownLoadedMomoryStream As MemoryStream = f.DownLoadFileStream
    26.  
    27.  
    28.         Dim WritingByteArray(1048576) As Byte
    29.  
    30.         Dim DownLoadedStreamLength As Long = DownLoadedMomoryStream.Length - 1
    31.         Dim BytesRed As Integer = 0
    32.  
    33.         'the OutPut File Stream to write
    34.         Dim WritingFileStream As New FileStream(downloaddestination, FileMode.OpenOrCreate, FileAccess.ReadWrite)
    35.  
    36.  
    37.         Using WritingFileStream
    38.  
    39.  
    40.             If Not DownLoadedMomoryStream Is Nothing Then
    41.  
    42.                 ' Reset the DownLoadedMomoryStream Position to 0,
    43.                 ' Since Initialized  @ Dim DownLoadedStreamLength As Long = DownLoadedMomoryStream.Length - 1
    44.                 DownLoadedMomoryStream.Position = 0
    45.  
    46.                 'TODO:
    47.                 ' DownLoadedMomoryStream.Position Becoming Equal to its size
    48.                 ' After the BytesRed = DownLoadedMomoryStream.Read(WritingByteArray, 0, 1048576)
    49.                 ' & Hence the While loop Terminates
    50.                 ' & Bgw_Download.ReportProgress Remains @ 1 %
    51.  
    52.                 While DownLoadedMomoryStream.Position < DownLoadedStreamLength
    53.  
    54.                     BytesRed = DownLoadedMomoryStream.Read(WritingByteArray, 0, 1048576)
    55.                     WritingFileStream.Write(WritingByteArray, 0, BytesRed)
    56.  
    57.                     ' Report the progress to back ground worker named Bgw_Download
    58.                     Bgw_Download.ReportProgress(Math.Round(DownLoadedMomoryStream.Position / DownLoadedStreamLength, 0))
    59.  
    60.  
    61.                 End While
    62.  
    63.             End If
    64.  
    65.             WritingFileStream.Close()
    66.             WritingFileStream.Dispose()
    67.  
    68.         End Using
    69.  
    70.  
    71.     End Sub
    Last edited by make me rain; Jan 18th, 2020 at 09:43 PM. Reason: "" to \ missing
    The averted nuclear war
    My notes:

    PrOtect your PC. MSDN Functions .OOP LINUX forum
    .LINQ LINQ videous
    If some one helps you please rate them with out fail , forum doesn't expects any thing other than this

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width