Is there a way to upload a local file selected with openfilerequester, to the server?
I guess it should be done with a httprequest, but I don't seem to get the headers right...
Uploading file to server
Re: Uploading file to server
You can't do this natively with Spiderbasic.
I have been able to do this using some javascript in Spiderbasic.
I use python cgi on the server end (on a Raspberry Pi) to handle the upload.
Below is some test code for a project I was working on.
It traps the file type and size before upload so you can process as needed.
It creates a standard webform and redirects post to a hidden iframe. I use the iframe for getting the status of the upload back from the cgi script.
It does this by injecting html into a Spiderbasic gadget.
Keep in mind this viewtopic.php?f=6&t=292#p882
Spiderbasic code
Python code
I have been able to do this using some javascript in Spiderbasic.
I use python cgi on the server end (on a Raspberry Pi) to handle the upload.
Below is some test code for a project I was working on.
It traps the file type and size before upload so you can process as needed.
It creates a standard webform and redirects post to a hidden iframe. I use the iframe for getting the status of the upload back from the cgi script.
It does this by injecting html into a Spiderbasic gadget.
Keep in mind this viewtopic.php?f=6&t=292#p882
Spiderbasic code
Code: Select all
Global upname.s
Global upnamekeep.s
Procedure.i GadgetElement(gadget, usejquery.b=#True)
Protected gadgetobject=GadgetID(gadget)
!return (v_gadgetobject && v_gadgetobject.div)? v_usejquery? $(v_gadgetobject.div):v_gadgetobject.div:null;
EndProcedure
If OpenWindow(0, 10,10,500,300 , "TEST")
TextGadget(3, 20, 20, 300, 80, "")
TextGadget(4,20,110,200,10,"")
element=GadgetElement(3,1)
EnableJS
$(v_element).html("<iframe id='upload_target' name='upload_target' src='' style='width:0px;height:0px;border:0px solid #ccc;'></iframe><form id='file_upload_form' method='post' name='myForm' action='http://192.168.0.121/cgi-bin/up.py' enctype='multipart/form-Data'>Select file : <input type='file' name='file' id='flupload'/><input value='Send File' type='submit' id='subby' ><button type='reset' id='file_reset' style='display:none'></form>");
$('#subby').hide();
DisableJS
!$(document).ready(function() {
; add click event to submit button
;
! $( "#subby" ).on( "click", function() {
!$('#subby').hide();
! });
! $( "#flupload" ).on( "click", function() {
SetGadgetText(4,"")
! });
! $('#file_upload_form').prop('target', 'upload_target');
! $("#flupload").change(function ()
! {
! var v_isize = ($("#flupload")[0].files[0].size );
! var v_upname = ($("#flupload")[0].files[0].name );
Debug isize
Debug upname
upnamekeep=upname
If FindString(LCase(upname),".jpg")=0
! $('#file_reset').trigger('click');
!$('#subby').hide();
SetGadgetText(4,"Bad file")
Else
!$('#subby').show();
EndIf
!$('#upload_target').load(function(){
iframehtml.s=""
! v_iframehtml=$('#upload_target').contents().find("body").html();
If FindString(iframehtml,"was uploaded successfully")
!$('#upload_target').contents().find('html').html("")
Debug iframehtml
SetGadgetText(4,"Upload OK for "+upnamekeep)
! $('#file_reset').trigger('click');
Else
If Len(iframehtml)<>0 And FindString(iframehtml,"was uploaded successfully")=0
Debug iframehtml
SetGadgetText(4,"Upload failed for "+upnamekeep)
EndIf
EndIf
!});
;}
! });
!});
EndIf
Python code
Code: Select all
#!/usr/bin/env python
import cgi, os
import cgitb; cgitb.enable()
try: # Windows needs stdio set for binary mode.
import msvcrt
msvcrt.setmode (0, os.O_BINARY) # stdin = 0
msvcrt.setmode (1, os.O_BINARY) # stdout = 1
except ImportError:
pass
form = cgi.FieldStorage()
# A nested FieldStorage instance holds the file
fileitem = form['file']
# Test if the file was uploaded
if fileitem.filename:
# strip leading path from file name to avoid directory traversal attacks
fn = os.path.basename(fileitem.filename)
open('/home/pi/files/' + fn, 'wb').write(fileitem.file.read())
message = 'The file "' + fn + '" was uploaded successfully'
else:
message = 'No file was uploaded'
print """\
Content-Type: text/html\n
<html><body>
<p>%s</p>
</body></html>
""" % (message,)
Re: Uploading file to server
This is great! Thanks a lot
Re: Uploading file to server
4 Years later... can uploading a file to a server be done natively in Spider Basic ???
Re: Uploading file to server
I'm afraid not.Paul wrote:4 Years later... can uploading a file to a server be done natively in Spider Basic ???
But falsam has programmed an UploadGadget in the meantime: https://github.com/falsam/UpLoadGadget
Greetings ... Peter
Re: Uploading file to server
Hi Peter,
The UploadGadget looks very interesting but since I am not a javascript guru, I want to be able to send the values along with 1 or 2 files all at once and I can't see how to duplicate this functionality...
The UploadGadget looks very interesting but since I am not a javascript guru, I want to be able to send the values along with 1 or 2 files all at once and I can't see how to duplicate this functionality...
Code: Select all
<form action='https://127.0.0.1/cgi-bin/mydata.cgi' method='post' enctype='multipart/form-data'>
Subject<br>
<input type='text' name='subject'><br>
<input type='hidden' name='pass' value='test data'><br>
<input type='file' name='file1' id='file1'><br>
<input type='file' name='file2' id='file2'><br>
<br>
<input type='reset' value='Clear' name='clear'>
<input type='submit' value='Send' name='senddata'>
</form>
Re: Uploading file to server
Hello Paul,
the UploadGadget has a parameter "MaxFiles". With this you should be able to upload more than one file at once.
Greetings ... Peter
the UploadGadget has a parameter "MaxFiles". With this you should be able to upload more than one file at once.
Greetings ... Peter
Re: Uploading file to server
When you add one or more files using the UploadGadget, they instantly upload.
This is not the behavior required.
I need all information entered, files selected, then finally a button press to send all data & files.
So simple to do with HTML code
This is not the behavior required.
I need all information entered, files selected, then finally a button press to send all data & files.
So simple to do with HTML code