Upload pdf from Desktop app to Web App

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
2 Likes