Just starting out? Need help? Post your questions and find answers here.
Joestes
 
Posts: 8
Joined: Thu Sep 17, 2015 11:14 am

Uploading file to server

by Joestes Thu Sep 17, 2015 11:16 am

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... :(
e2robot
 
Posts: 38
Joined: Wed Mar 19, 2014 8:34 am

Re: Uploading file to server

by e2robot Fri Sep 18, 2015 2:00 am

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

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,)
Joestes
 
Posts: 8
Joined: Thu Sep 17, 2015 11:14 am

Re: Uploading file to server

by Joestes Fri Sep 18, 2015 6:41 am

This is great! Thanks a lot
User avatar
Paul
 
Posts: 57
Joined: Wed Feb 26, 2014 6:46 pm
Location: Canada

Re: Uploading file to server

by Paul Fri Oct 04, 2019 3:49 am

4 Years later... can uploading a file to a server be done natively in Spider Basic ???
User avatar
Peter
 
Posts: 625
Joined: Mon Feb 24, 2014 10:17 pm
Location: 127.0.0.1

Re: Uploading file to server

by Peter Fri Oct 04, 2019 6:47 am

Paul wrote:4 Years later... can uploading a file to a server be done natively in Spider Basic ???

I'm afraid not.

But falsam has programmed an UploadGadget in the meantime: https://github.com/falsam/UpLoadGadget

Greetings ... Peter
User avatar
Paul
 
Posts: 57
Joined: Wed Feb 26, 2014 6:46 pm
Location: Canada

Re: Uploading file to server

by Paul Fri Oct 04, 2019 1:44 pm

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...
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'>
  &nbsp;&nbsp;&nbsp;&nbsp;
  <input type='submit' value='Send' name='senddata'>
</form>
User avatar
Peter
 
Posts: 625
Joined: Mon Feb 24, 2014 10:17 pm
Location: 127.0.0.1

Re: Uploading file to server

by Peter Fri Oct 04, 2019 9:28 pm

Hello Paul,

the UploadGadget has a parameter "MaxFiles". With this you should be able to upload more than one file at once.

Greetings ... Peter
User avatar
Paul
 
Posts: 57
Joined: Wed Feb 26, 2014 6:46 pm
Location: Canada

Re: Uploading file to server

by Paul Fri Oct 04, 2019 10:11 pm

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 :(
Return to Coding Questions

Who is online

Users browsing this forum: No registered users and 1 guest