Simple Download/Upload page.

44 views
Skip to first unread message

Florent Barra

unread,
Dec 11, 2013, 10:15:03 AM12/11/13
to nagare...@googlegroups.com
Hi, 

I'm Trying to make a simple page for downloading  and uploading items in a folder.
but with both actions, i have an issue.

For Downloading (i used this help):
i have this type of error :"The resource could not be found. /barraf/nagare/exple_02/files/img_03.jpg".
But this path is used to make the items list.

For Uploading:
i try a lot of different codes but none worked. my variable self.file is still empty.

sorry but i don't have any ideas to make my app work properly.
(in attachment,  my basic work.)
exple_02.tar.gz

Alain Poirier

unread,
Dec 11, 2013, 12:34:01 PM12/11/13
to nagare...@googlegroups.com
Hi,

Le 11 déc. 2013 à 16:15, Florent Barra <floren...@gmail.com> a écrit :

> Hi,
>
> I'm Trying to make a simple page for downloading and uploading items in a folder.
> but with both actions, i have an issue.
>
> For Downloading (i used this help):
> i have this type of error :"The resource could not be found. /barraf/nagare/exple_02/files/img_03.jpg".
> But this path is used to make the items list.

It’s because you haven’t any action registered for the link line #25, only a
'href’ is defined:

h.a('Dl', href='/barraf/nagare/exple_02/files/'+i)

so the browser simply tries to go to the URL '/barraf/nagare/exple_02/files/<filename>'.


First register an action which will pass the filename to download to 'read_file()':

h.a('Dl').action(lambda i=i: self.read_file(i))

Seconde, raise a 'webob.exc.HTTPOk()' response when you want to directly send
data to the browser, bypassing the normal DOM rendering phase of Nagare:

def read_file(self, filename):
filename = '/barraf/nagare/exple_02/exple_02/files/' + filename

content_type = 'image/' + imghdr.what(filename)
with open(filename) as f:
raise exc.HTTPOk(body=f.read(), content_type=content_type)

(here the standard 'imghr' module is used to find the mime type of the image)

> For Uploading:
> i try a lot of different codes but none worked. my variable self.file is still empty.

The line #31:

h << h.input(type='file').action(self.file)

doesn’t registers an action but only the value of the variable 'self.file' which
is 'None'.


A 'nagare.var.var' object is an easy way to register an action to set a variable:

http://www.nagare.org/trac/wiki/CallbacksAndForms#the-var-var-objects

So first, change:

def __init__(self):
self.file = None

by

def __init__(self):
self.file = var.Var()

Then, the method 'write_file()' becomes :

def write_file(com):
upload = self.file()
if upload != '':
with open('/barraf/nagare/exple_02/exple_02/files/test.jpg', 'w') as file:
file.write(upload.value)

That’s all :)

> sorry but i don't have any ideas to make my app work properly.
> (in attachment, my basic work.)

Here is the complete modified code (in this version I even use a local 'upload'
variable instead of ‘self.file'):


from nagare import presentation, var
from webob import exc
import os, re, cgi, imghdr

class App:
def list_file(self):
files = [f for f in os.listdir('/barraf/nagare/exple_02/exple_02/files/') if re.match(r'.*', f)]
return files

def read_file(self, filename):
filename = '/barraf/nagare/exple_02/exple_02/files/' + filename

content_type = 'image/' + imghdr.what(filename)
with open(filename) as f:
raise exc.HTTPOk(body=f.read(), content_type=content_type)

def write_file(self, comp, upload):
if upload != '':
with open('/barraf/nagare/exple_02/exple_02/files/test.jpg', 'w') as file:
file.write(upload.value)



@presentation.render_for(App)
def render(self, h, comp, *args):
upload = var.Var()
with h.fieldset:
h << h.legend('Download section :')
for i in self.list_file() :
h << i << ' ' << h.a('Dl').action(lambda i=i: self.read_file(i))
h << h.br

with h.fieldset:
h << h.legend('Upload section :')
with h.form:
h << h.input(type='file').action(upload)
h << h.br
h << h.input(type='submit', value='Send').action(lambda: self.write_file(comp, upload()))
return h.root

app = App

Florent Barra

unread,
Dec 13, 2013, 2:27:11 AM12/13/13
to nagare...@googlegroups.com


Thank you Alain. 
 
Reply all
Reply to author
Forward
0 new messages