Does anyone have some working example code to upload a pdf file from a xojo desktop app to a xojo webapp. I have tried the Curl example from MBS but can not get it work. My app is working if I send Json to the webapp using URLConnection. But now I need to send some small pdf files. I am using Xojo version 2020 R2.1 and I did check, the Curl plugin is loading. The sending app just locks up and never sends anything when using the MBS example. Thanks
Would it work to read the pdf file, Base64 it and stick it in a json message?
I think that might work if the pdf is a small file, and they usually are. I am creating the pdf myself using pdfdocument. Then I save the pdf and send a copy via email attachment to my customer. I then want to archive the pdf on the server. The app that sends the file is a desktop app on a laptop that is mobile.
I just need some code to get started on this file upload. I guess that I can use PDFfolderItem.Base64Data but do I need to append anything else? Like the application type?
This might help.
Web Code (HandleURL)
Function HandleURL(Request As WebRequest, Response As WebResponse) Handles HandleURL as Boolean If Request.Path = "" Then Return False ' This is not a special URL request, so let the framework handle it End If // The URL will be /File?Name=<URLEncodedfilename> // the content will be binary data // NB This is only suitable for small files as the entire request will be uploaded into memory before being processed. If Request.Path <> "file" Then Return False ' A 404 response will be sent from the framework End If If Request.QueryString.BeginsWith("name=") Then Var Name As String = DecodeURLComponent(Request.QueryString.NthField("=", 2)) // Write the file to disk Var b As BinaryStream = BinaryStream.Create(SpecialFolder.Temporary.Child(Name)) b.Write(Request.Body) b.Close Response.Status = 200 Response.Write "Ok" Else Response.Status = 400 Response.Write("Invalid Request") End If Return True End Function
And the desktop code (Action event of a button) Uploader is a URLConnection dropped on the window
Sub Action() Handles Action Var d As New OpenFileDialog Var f As FolderItem = d.ShowModal() If f = Nil Then MessageBox("File not selected") Return End If Var b As BinaryStream = BinaryStream.Open(f) Var url As String = "HTTP://127.0.0.1:8080/file?Name=" + EncodeURLComponent(f.Name) Uploader.SetRequestContent(b.Read(b.Length), "application/octet-stream") Uploader.Send("POST", url) End Sub
Thanks Wayne. I think that is what I needed. I will give it try tomorrow.
Wayne’s solution is working great. I also tried a larger pdf up to 1 mb and no problems.