Memoryblock contains a file, how will I save this file to a folder on windows

Hi,

So I am using the RB-libcURL XOJO library made by charron0 to GET a file from a cloud storage api.
I got to the point where I can let a Classic MemoryBlock contain the file that I just downloaded, I was wondering if anyone could pop me a hint or two on how I would go about saving this file in the MemoryBlock to a folder on my computer.

This is what i’ve got…

//Memoryblock that contains my file

      Dim responseData As MemoryBlock
      responseData = cURL.GetDownloadedData

//My try-hard piece of work to try and store the file in a folder(Please don't judge) :)

      Dim ReadStream As New BinaryStream(responseData)
      Dim WriteFile as FolderItem = GetSaveFolderItem("", "")
      If writeFile <> Nil Then
        Dim writeStream As BinaryStream = BinaryStream.Create(writeFile, True)
        writeStream.Write(ReadStream)
        writeStream.Close
      End If
      readStream.Close

Any help would be greatly appreciated!
Thanks in advance.

  • Dian

Something like this might do it:

Dim WriteFile as FolderItem = GetSaveFolderItem("", "")
If writeFile <> Nil Then
 Dim writeStream As BinaryStream = BinaryStream.Create(writeFile, True)
 writeStream.Write(responseData)
 writeStream.Close
End If

You can also pass the writeStream as the optional second parameter to the GET method to download directly to the file, bypassing the MemoryBlock:

[code]Dim WriteFile as FolderItem = GetSaveFolderItem("", “”)
If writeFile <> Nil Then
Dim writeStream As BinaryStream = BinaryStream.Create(writeFile, True)

Dim cURL As New cURLClient
If Not cURL.Get(“http://www.example.com”, writestream) Then
MsgBox(“Error”)
End If

writeStream.Close
End If[/code]

I don’t see where you’re passing in the file name.

Personally I would go for something like the following.

[code]Dim WriteFile as FolderItem = specialFolder.temporary.child( “download-xyz” )
Try
Dim writeStream As BinaryStream = BinaryStream.Create(writeFile, True)
writeStream.Write(responseData)
writeStream.Close

// — Add code here to process the file.

Catch err as IOException
Msgbox "Failed because " + err.message

End Try
[/code]

The file name comes from GetSaveFolderItem.

I am almost there, thanks all for the input, @Andrew Lambert has led me to the best solution so far, the only unfortunate thing I’m still stuck at is the file that has been saved somehow says “This file is not supported” which in this case is a .PNG image I am testing with, does it have something to do with FileTypes of somekind(I don’t have knowledge on working with files in xojo haha)

Thanks,

Wait nevermind haha stuped mistake, thanks everyone, appreciate the input!