WebUploadedFile Trouble - trailing line added - XML trouble

Here’s a hack you can use to clean up the extra garbage at the end of the file. It may not work in every case, but it seems to work in my limited testing with Safari, Chrome, and FireFox:

Private Function CleanUploadData(data as string) As string

  // see https://forum.xojo.com/t/webuploadedfile-trouble-trailing-line-added-xml-trouble/62564/3
  // remove garbage at end of file from WebFileUploader in Xojo 2021R1
  
  // it's a leftover mime-multipart separator
  //  format is always
  // 0D0A2D2D[...]2D2D[WebKitFormBoundaryXXXX or just random integer]2D2D0D0A
  
  // examples:
  // -----------------------------416884627533806944232202863060--
  // ------WebKitFormBoundaryIecXIus2umVD7p2U--
  
  dim mb as MemoryBlock = data  // convert string to memoryblock
  
  dim u as integer = mb.Size -1
  dim x as integer = u
  
  dim minX as integer = max(0, u - 256) // don't scan before first byte of file, and only check the last 256 bytes
  
  if _
    mb.StringValue(x-3,1) = "-" and _
    mb.StringValue(x-2,1) = "-" and _
    mb.StringValue(x-1,1) = chr(&h0D) and _
    mb.StringValue(x-0,1) = chr(&h0A) then
    
    
    // ending matches, backtrack now to find start of boundary string
    x = u - 4
    
    while mb.StringValue(x,1) <> "-" and x > minX
      x = x -1
    wend
    
    // backtrack to find start of "-"
    while mb.StringValue(x,1) = "-" and x > minX
      x = x -1
    wend
    
    if x <= minX then
      app.Log "*** UploadHack: found end bytes but did not find start tag within 256 bytes of end of file"
      return data
    end if
    
    dim junk as string = mb.StringValue(x, u-x+1)
    
    app.Log "* UploadHack: extra bytes removed: '" + junk + "'" 
    
    data = mb.StringValue(0,x-1)
    
    
    
  end if
  
  return data
End Function