can we use JSONRPC to upload?

1,270 views
Skip to first unread message

Phyo Arkar

unread,
Feb 15, 2012, 4:16:07 AM2/15/12
to web2py
Out of curisority ,. can we use JSONRPC protocol to Upload / Download Files (Multipart/Form) ?

If so , how it will work?


Ross Peoples

unread,
Feb 15, 2012, 7:14:05 AM2/15/12
to web...@googlegroups.com
I've never tried before, but traditionally, you would encode the binary file into a base64 string, then decode it on the other end. I don't know if JSONRPC does this for you, or has a better way.

António Ramos

unread,
Feb 15, 2012, 7:25:24 AM2/15/12
to web...@googlegroups.com
I have something working with xmlrpc
In web2py i have this service!

@service.xmlrpc
def getFile(file,name):
db(db.files.id>0).delete()
lixo = glob.glob(os.path.join(request.folder,'uploads','*.txt'))
  for f in lixo:  # i delete all files in the folder then i receive the new one
os.remove(f)
filedata=cStringIO.StringIO(file.data)
file=db.files.file.store(filedata,name)
db.files.insert(file=file)
  return "Import - OK"



Then i have a script outside web2py with this code.
This code gets a csv file from an email i receive everyday in my inbox and sends it to my web2py server. 
Just notice the bold lines. The rest is Lotus Notes specific...

import xmlrpclib
import win32com.client
import sys
import time
filebin=None
session = win32com.client.Dispatch('Lotus.NotesSession')
session.Initialize('mypassword')
db = session.GetDatabase('SIGMA','mail/mymailbox.nsf')
view = db.GetView('CYGSA')
doc = view.GetFirstDocument()
try:
body=doc.GetFirstItem('Body')
file=doc.GetAttachment("LCOBROS.csv")
file.ExtractFile("c:\\LCOBROS.txt")
server = xmlrpclib.ServerProxy('http://10.0.0.100/ERP/default/call/xmlrpc')
filepath='c:\\'
filename='LCOBROS.txt'
filebin = xmlrpclib.Binary(open(filepath+filename).read())
print server.getFile(filebin,filename)
  doc.remove(False) # i delete the email with that attachment
except:
pass


2012/2/15 Ross Peoples <ross.p...@gmail.com>

Massimo Di Pierro

unread,
Feb 15, 2012, 9:30:24 AM2/15/12
to web2py-users
The problem is that xmlrpc only works for small files because needs to
serialize in XML. It would break for large files. The propotocol is
not designed for streaming.
> *server = xmlrpclib.ServerProxy('http://10.0.0.100/ERP/default/call/xmlrpc')
> *
> filepath='c:\\'
> filename='LCOBROS.txt'
> *filebin = xmlrpclib.Binary(open(filepath+filename).read())*
> print server.getFile(filebin,filename)
>   doc.remove(False) # i delete the email with that attachment
> except:
> pass
>
> 2012/2/15 Ross Peoples <ross.peop...@gmail.com>

Ross Peoples

unread,
Feb 15, 2012, 10:09:52 AM2/15/12
to web...@googlegroups.com
The best way might be to use JSONRPC to get an authorization code, which is then POSTed along with the binary contents in a separate HTTP request.

Massimo Di Pierro

unread,
Feb 15, 2012, 11:55:13 PM2/15/12
to web2py-users
+1
Reply all
Reply to author
Forward
0 new messages