sending a file to a webpage (.gambas cgi)

For questions about Gambas web tools.
Post Reply
User avatar
BruceSteers
Posts: 1691
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

sending a file to a webpage (.gambas cgi)

Post by BruceSteers »

How can i send a file to a webpage?

I want in my gambas application to be able to open a URL to a gambas cgi (gb.web) and post a file to it.

So on the computer side is a gambas program that wants to send file data.
And on the server end it receives the file data so it can then save it as a file.

The program and the online server are mine so I am free to do this another way if there is a better way.

It is for my Blockski+ program , I am making a Highscore repository/server so players can easily merge scoreboards.

I currently have it so i can HttpClient.Download() a .gambas cgi with various parameters so my program can simply download urls like

web.address/cgi-bin/Scores.gambas?getusers
web.address/cgi-bin/Scores.gambas?getscores=username

getusers just prints available users and getscores prints the users scores.
this is easy , the .gambas cgi just prints text so the result of the download it the text i want.

But how to reverse this so i can send a whole file.

I've tried httpClient.Post() but cannot get it to work.
I could do with an example of how to post data using httpClient.class

Cheers in advance for any tips
If at first you don't succeed , try doing something differently.
BruceS
User avatar
Got2BeFree
Posts: 109
Joined: Saturday 26th November 2016 2:52am
Location: Lost

Re: sending a file to a webpage (.gambas cgi)

Post by Got2BeFree »

About 9 years ago I created an app to keep track of my parents doctors appointment. The app would also create an HTML file and upload that file to my server via FTP. I was then able to give the password protected URL link to siblings so they could also know the schedule. I don't remember if I uploaded to the exact location or if I moved it via a chron job. If I remember correctly, I tried to do it something like what you are trying but never was able to make it work so I went with the FTP route.

Maybe you can upload the file by FTP, and then move it to the proper location?
sholzy

I'm wondering around lost in the past, not knowing where the present is.
User avatar
BruceSteers
Posts: 1691
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: sending a file to a webpage (.gambas cgi)

Post by BruceSteers »

I can't use FTP as the program will be in the public domain.
So I Cannot really add my ftp login details to the app.

Needs to be sent by http push or post I think
If at first you don't succeed , try doing something differently.
BruceS
User avatar
thatbruce
Posts: 197
Joined: Saturday 4th September 2021 11:29pm

Re: sending a file to a webpage (.gambas cgi)

Post by thatbruce »

I think, if I understand, you need a public ftp directory on your server, i.e. one that the public can upload files to, and which should be very isolated from the rest of your server. This is possible, don't ask me, I threw my ftp manual out a decade ago.
Then you can write the stuff in your page to upload to that location via ftp. I think Gambas can handle that.
Now getting stuff out of there is a different kettle of fish.
(Caveat) It is about 10 or so years since I was doing that stuff.
hth
b

p.s. IIRC http is not for transferring files, just a bunch of text with a bunch of markup tags. Post is just the same and I've not heard of push but I'd guess ditto. ftp is OTOH for transferring files, maybe that's why they called it that. :)

p.p.s. Maybe wade through the jcrap on a site like https://www.flaticon.com/ to see how they download files to the user pc,

p.p.p.s. I sit corrected. Maybe https://medium.com/@API4AI/post-a-file- ... 3fb70a3f73 could help
I have just received a $400 bottle of Tasmanian whiskey. I am to be considered "very happy". :D
User avatar
BruceSteers
Posts: 1691
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: sending a file to a webpage (.gambas cgi)

Post by BruceSteers »

cheers Mr b
multipart/form-data looks promising

But what i don't know is how to process the request server-side

Here is what my program does to send the data


Public Sub btnUpload_Click()

  Dim sRes As String
  Dim s As String = File.LoadArchive(File.SetExt(ESettings.Path, "zx"), True)

  With $hTp = New HttpClient As "HTP"
    .URL = $sHost &/ $sScorePath & "submit"
    .Post("multipart/form-data", s)
  End With

End



the URL looks something like
127.0.0.1/cgi-bin/Scoreprog.gambas?submit

I can detect the "submit" in the QUERY_STRING server-side but then i do not know how to find the data sent? it does not seem to be in any CGI[Key] variable.
the client-side shows bytes are sent but how do i find the data server-side?

Cheers
If at first you don't succeed , try doing something differently.
BruceS
User avatar
gbWilly
Posts: 97
Joined: Friday 23rd September 2016 11:41am
Location: Netherlands
Contact:

Re: sending a file to a webpage (.gambas cgi)

Post by gbWilly »

(Somewhat corrected post of my reply a second ago to this question but on the gambas mailing list)

So what you are asking is, where is the file on the server or what?

I see: As server side can detect the submit, you know on server side that upload happened.
Now you want to go to the file from server side I presume, to do some stuff.
My guess is that the web server configuration determines the upload location and access rights to upload to there
And I guess your upload on client side determines the filename.

These are just some wild guesses that might set you on the right path (or not).

Haven' t used it myself, did use the opposite, downloading from server.

gbWilly
gbWilly
- Dutch translation for Gambas3
- Gambas wiki content contributer
- Gambas3 Debian repository


... there is always a Catch if things go wrong!
User avatar
BruceSteers
Posts: 1691
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: sending a file to a webpage (.gambas cgi)

Post by BruceSteers »

Seems i was missing Request.class and the Request.Post property

(who'd have thought that to process the request of posted data i had to use Request.Post :roll: )

The solution was the following....
Set
Request.Debug = True
send data with content type multipart/form-data

then server-side i can simply use
Dim vData As Variant
vData = Request.Post.Contents


Easy as that :)

EDIT:
actually I found that way truncated data.

The following way worked much better and there is no need to set Request.Debug = True when doing it like this

Dim sTxt As String
sTxt = Read Request.ContentLength
If at first you don't succeed , try doing something differently.
BruceS
Post Reply