Sending .jpg files via UrlConnection fails sometimes

I have an app on mobile windows tablets that sends .jpg pictures files to server webapp via UrlConnection. About 50% of the time the file is saved on the server as a 0kb file and the other 50% of time it works fine. The app also sends .pdf files the same way but they are always saved on the server correctly. Here is the coded that I use to send the files. Can someone tell me what I am doing wrong?
The picture files are less than 500kb size and I always get 200 “OK” response back to the mobile app.
My tests from my laptop via Wifi always works, so it is very hard for me to debug.

WinMain.ArchivePdfConnector.ClearRequestHeaders //This is the URLConnect opject that is placed on the window
var sql as string
var Rs as rowset
var filename as string
var fi as FolderItem
sql = "Select * from Signature where SendDone IsNull or SendDone = '' Order By ID DESC LIMIT 1"

  rs = DB1.SelectSQL(SQL)
  if rs <> Nil  then
    if rs.AfterLastRow then
      filename = rs.column("FolderPath").StringValue
      //The filename is saved as .png becasuse the signature file is .png but it is the same filename as the workorder .pdf
      //So to get the correct .pdf filename, I need to change the extension on the filename from the database.
      //All the .jpg pictures filenames are also saved in the Signature table.
      filename = filename.replace(".png", ".pdf")//all jpg's are sent also because they are not png's
      fi = new FolderItem(filename)
      sendDone = rs.Column("ID").IntegerValue
      if fi.Exists then
        //this will send a .pdf or .jpg file, one at a time
        Var b As BinaryStream = BinaryStream.Open(fi)
        Var url As String = WWWAddress + "/ArchivePdfFile?Name=" + EncodeURLComponent(fi.Name)
        WinMain.ArchivePdfConnector.AllowcertificateValidation = True
        WinMain.ArchivePdfConnector.RequestHeader("Authorization") = "Basic " + EncodeBase64(userp)
        WinMain.ArchivePdfConnector.SetRequestContent(b.Read(b.Length), "application/octet-stream")
        WinMain.ArchivePdfConnector.Send("POST", url)
        db1.ExecuteSQL("update Signature set SendDone = -1 where ID = ?", sendDone)
      end if
    end if
  end if
  //See rsocket ArchivePdfConnector for the code to see what is returned
catch e as RuntimeException
end try

This is the code on the webapp that writes the file to the disc.

If Request.QueryString.BeginsWith("name=") Then // this is the name of the file
  Var Name As String = DecodeURLComponent(Request.QueryString.NthField("=", 2))
  // Write the file to disk
    Var myDataFolder As FolderItem = SpecialFolder.SharedPreferences.Child("EzProService")
    if not myDataFolder.Exists then
    end if
    Var b As BinaryStream = BinaryStream.Create(myDataFolder.Child(Name), True)
    Response.Status = 200
    Response.Write "Ok"
  catch e as IOException
    Response.Status = 400
    Response.Write("Can not write file")
    Return True
  end try
  Response.Status = 400
  Response.Write("Invalid Request")
End If
Return True

It doesn’t resolve the problem, but you could test for Request.Body being blank and return an error.

You may also want to check b.Length <> 0. Could it be a timing issue where you’re trying to send the file before it actually is written/flushed to disk?

The files are saved on the tablet way before they are sent to the webapp, so I know that is not the issue.
I could check for 0 length file before I send it, but what would cause it to be 0 length? I know the file exists on the tablet (I have checked that).
Thanks for the suggestions.

You mentioned the word “Flushed” in your reply. So it got me thinking.
I added b.flush after b.Write(Request.Body) and now it seems to be working great.
I marked you reply as the answer.
Thanks for help.

1 Like

This has helped me too as I am having issues sending HTML to the server with a PDF being returned and sometimes the returning file is truncated.

1 Like