0

I have a service which provides a stream. This stream should be written to a pdf-file.

I tried this method but it didn't work:

Using hcwHandler As IHealthCareWorkerServiceHandler = Container.CurrentContainer.Resolve(Of IHealthCareWorkerServiceHandler)()
  stream = hcwHandler.DownloadPrescription(New DownloadPrescriptionRequest With {
    .ProfessionCode = ucSelectProfession.ProfessionCode,
    .RizivNumber = ucSelectProfession.Nihdi,
    .Culture = language
  }).Result
End Using

Dim buffer As Byte() = Encoding.ASCII.GetBytes(stream.ToString())

Using ms As New MemoryStream(buffer)
  'write to file
  Using file As New FileStream("prescription.pdf", FileMode.Create, FileAccess.Write)
    ms.WriteTo(file)
  End Using
End Using

I have tried several other solutions as well, but none seemed to work. I never get a file.

Can anyone help me?

InteXX
  • 6,135
  • 6
  • 43
  • 80
Bart Schelkens
  • 1,235
  • 4
  • 21
  • 45

2 Answers2

0

You can read the buffer (if there is data there) in a Using block without MemoryStream (like code below shows) and put a full absolute path like C:\Documents\PdfFolder\prescription.pdf

Using saveFileDialog1 As New SaveFileDialog()
  saveFileDialog1.InitialDirectory = "C:\Documents"
  saveFileDialog1.RestoreDirectory = True
  saveFileDialog1.Filter = "PDF files|*.pdf" ' change here for csv or xls
  saveFileDialog1.FileName = "yourDefaultfileName.pdf"

  If saveFileDialog1.ShowDialog <> Windows.Forms.DialogResult.Cancel Then
    If Len(saveFileDialog1.FileName.Length) > 0 Then
      Try
        Dim pdfPath As String = System.IO.Path.GetFullPath(saveFileDialog1.FileName)

        Using file As New FileStream(pdfPath, FileMode.Create, FileAccess.Write)
          file.Write(buffer, 0, buffer.Length)
        End Using

        MessageBox.Show("Saved in " & pdfPath)

      Catch
        MsgBox("Not a valid name file.")

      End Try
    End If
  End If
End Using
InteXX
  • 6,135
  • 6
  • 43
  • 80
G3nt_M3caj
  • 2,497
  • 1
  • 14
  • 16
  • Usually when you download something in the browser, you can see that (in Google Chrome) at the bottom of the browser. How can I do that with the file I'm writing? I can find the file now, so it is being written. – Bart Schelkens Nov 18 '19 at 10:45
  • And remember what @Andrew Morton says about file corruption. You need to treat stream directly/in another way – G3nt_M3caj Nov 18 '19 at 10:55
  • @G3nt_M3caj Perhaps you could convert the C# code from [How do I save a stream to a file in C#?](https://stackoverflow.com/q/411592/1115360) into VB.NET for the OP too, to make a complete answer ;) – Andrew Morton Nov 18 '19 at 11:03
  • Yes, done. I hope OP understand "yourStreamHere" in this case must to be a Stream (her stream) and not a path :). – G3nt_M3caj Nov 18 '19 at 11:20
0

And here a VB.NET version of How do I save a stream to a file in C#

Using fromWebFileStream As Stream = New StreamReader(yourStreamHere)
  Using localFileStream As FileStream = File.Create("C:\Documents\PathTo\File")
    'if you aren't at the begin of your stream uncomment this
    'fromWebFileStream.Seek(0, SeekOrigin.Begin)
    fromWebFileStream.CopyTo(localFileStream)
  End Using
End Using
InteXX
  • 6,135
  • 6
  • 43
  • 80
G3nt_M3caj
  • 2,497
  • 1
  • 14
  • 16