WebFile Image Download (Web1.0)

I show the user an image in a modal and want to allow download of the image (without having to save it server side first) but am having issues.

The image data is base64 and stored in an SQLite db and I am using the below code to actually show the image (shown here with the debugger showing the image data string):

NOTE: I have tried removing the ‘header’ string info “data:image/png;base64,” but still get a file that is reportedly not recognised. Below is the code I am using for this:

// download current image
imgfile = new WebFile
imgfile.Filename = "image.png"
imgfile.MIMEType = "image/png"
imgfile.ForceDownload = true

'dim p as picture = StringToPicture(largeImg1.imgdata)

'if p <> nil then
  
  imgfile.data = largeImg1.imgdata //imgdata is a string (of the type data:image/png;base64, (but have tried removing this 'header'))
  
  showurl imgfile.URL
  
   ' end if

Any thoughts ?

EDIT: DecodeBase64 first I guess ?

1 Like

As so often happens when writing a question, the answer reveals itself after posting. For anyone else, I needed to remove the ‘HTML’ png header portion of the string:

“data:image/png;base64,”

Then simply decode the resultant string and it works fine. Corrected working code:

// download current image
imgfile = new WebFile
imgfile.Filename = "image.png"
imgfile.MIMEType = "image/png"
imgfile.ForceDownload = true

imgfile.data = DecodeBase64(largeImg1.imgdata) //imgdata is a string (with "data:image/png;base64," removed)

showurl imgfile.URL