Trouble with creating static file on the fly.

774 views
Skip to first unread message

JoeCodeswell

unread,
Jan 19, 2012, 11:12:53 AM1/19/12
to web2py-users
Dear web2py folks,

I am having trouble creating a static file on the fly.

Here is the code in dnload_file/controllers/default.py.

def make_dl():
myurl = URL('static', 'excel.txt')
f = open(myurl,'w')
for i in range(20):
f.write('This is a test %2s\n'%(i))
f.close()
return dict(myurl=myurl)

Here is the ticket Traceback.
Traceback (most recent call last):
File "E:\web2py\gluon\restricted.py", line 194, in restricted
exec ccode in environment
File "E:/web2py/applications/dnload_file/controllers/default.py",
line 80, in <module>
File "E:\web2py\gluon\globals.py", line 149, in <lambda>
self._caller = lambda f: f()
File "E:/web2py/applications/dnload_file/controllers/default.py",
line 21, in make_dl
f = open(myurl,'w')
IOError: [Errno 2] No such file or directory: '/dnload_file/static/
excel.txt'

Any ideas?

Thanks for the help in advance.

Love and peace,

Joe

Anthony

unread,
Jan 19, 2012, 11:25:59 AM1/19/12
to web...@googlegroups.com


On Thursday, January 19, 2012 11:12:53 AM UTC-5, JoeCodeswell wrote:
Dear web2py folks,

I am having trouble creating a static file on the fly.

Here is the code in dnload_file/controllers/default.py.

def make_dl():
    myurl = URL('static', 'excel.txt')
    f = open(myurl,'w')

URL() generates a relative URL, but you need to generate a filesystem path, so try:

import os
myfile = os.path.join(request.folder, 'static', 'excel.txt')
f = open(myfile, 'w')

Anthony
 

JoeCodeswell

unread,
Jan 19, 2012, 12:39:58 PM1/19/12
to web2py-users
Dear Anthony,

Thanks for the tip. No I am able to create a file for download on the
fly.

Here's the final code.

def make_dl():
import os

myurl = URL('static', 'excel.txt')
myfile = os.path.join(request.folder, 'static', 'excel.txt')
f = open(myfile,'w')
for i in range(20):
f.write('This is a test %2s\n'%(i))
f.close()
# forces
download NOT streaming
mylink = XML(A('clickme to download',_href=myurl+'?attachment'))
return dict(mylink=mylink)

Thanks again, Anthony.

Love and peace,

Joe
Reply all
Reply to author
Forward
0 new messages