Skip to content
Snippets Groups Projects
Commit 978da8d6 authored by Dorothee Hueser's avatar Dorothee Hueser
Browse files

File download

parent e074c116
No related branches found
No related tags found
No related merge requests found
......@@ -586,8 +586,53 @@ $def with (data)
<p><a href="getusrparams">${data['indexmessage3']}</a></p>
```
then sessionend.html:
```
```html
<h2>Finished</h2>
<p><a href="/">leave and keep parameters and results on server</a></p>
<p><a href="/logout">logout with deleting all parameters and results</a></p>
```
## Download of files
If the file readin is used like this
```python
class downloadtxt:
def GET(self):
filename = 'usr/' + session.get("username") + '/config.txt'
print(filename)
try:
return open(filename).read()
except:
return 'error 404: config.txt not found'
```
without specifying that it shall be an attachment, it will display the content in teh browser window of the client. To cause it to really download it such that the user at the client can store it in his downloads or open it with some chosen application, the following is needed:
`web.header('Content-Disposition', 'attachment; filename="config.txt"')`
like this
```
class downloadtxt:
def GET(self):
filename = 'usr/' + session.get("username") + '/config.txt'
print(filename)
try:
web.header('Content-Disposition', 'attachment; filename="config.txt"')
return open(filename).read()
except:
return 'error 404: config.txt not found'
```
and for a binary like a jpg image
```python
class downloadjpg:
def GET(self):
filename = 'usr/' + session.get("username") + '/samu_treppensteigen03.06.08003.jpg'
print(filename)
try:
web.header('Content-Disposition', 'attachment; filename="resultplot.jpg"')
web.header('Content-type','images/jpeg')
web.header('Content-transfer-encoding','binary')
return open(filename, 'rb').read()
except:
return 'error 404: resultplot.jpg not found'
```
<h2>Finished</h2>
<p><a href="/download">download result</a></p>
<p><a href="/">leave and keep parameters and results on server</a></p>
<p><a href="/logout">logout with deleting all parameters and results</a></p>
$def with (data)
$def with (title, welcome, linkname, linktext)
<h2>${data['indextitle']}</h2>
<p>${data['indexmessage1']}</p>
<p><a href="upload">${data['indexmessage2']}</a></p>
<p><a href="getusrparams">${data['indexmessage3']}</a></p>
<h2>${title}</h2>
<p>${welcome}</p>
$for i in range(0,len(linkname)):
<p><a href="${linkname[i]}">${linktext[i]}</a></p>
......@@ -13,11 +13,12 @@ import json
urls = (
'/', 'index',
'/upload', 'upload',
'/login', 'login',
'/logout', 'logout',
'/start', 'sessionstart',
'/getusrparams', 'getusrparams'
'/getusrparams', 'getusrparams',
'/upload', 'upload',
'/download', 'downloadtxt'
)
web.config.debug = False
......@@ -62,12 +63,17 @@ class logout:
class sessionstart:
def GET(self):
print('sessionstart with user '+session.username)
data = {}
data['indextitle'] = 'Foo'
data['indexmessage1'] = 'Welcome to Foo, '+session.username
data['indexmessage2'] = 'go to upload of ascii files'
data['indexmessage3'] = 'go to user input form'
return render.sessionstart(data)
linkname = {}
linktext = {}
title = 'Foo'
welcome = 'Welcome to Foo, '+session.username
linkname[0] = 'upload'
linktext[0] = 'go to upload of ascii files'
linkname[1] = 'getusrparams'
linktext[1] = 'go to user input form'
linkname[2] = 'download'
linktext[2] = 'download result'
return render.sessionstart(title, welcome, linkname, linktext)
class upload:
def GET(self):
......@@ -80,7 +86,7 @@ class upload:
for f in files:
# here in this example files to go to the directory usr/thisuser
target = os.path.join('usr', 'thisuser', f.filename)
target = os.path.join('usr', session.get("username"), f.filename)
content = f.file.read().decode('ascii', errors='ignore')
# here code could go to sanitize the content or to parse/convert the file or so ....
with open(target, 'w') as f:
......@@ -88,6 +94,28 @@ class upload:
return "Data files have successfully been uploaded"
class downloadjpg:
def GET(self):
filename = 'usr/' + session.get("username") + '/samu_treppensteigen03.06.08003.jpg'
print(filename)
try:
web.header('Content-Disposition', 'attachment; filename="resultplot.jpg"')
web.header('Content-type','images/jpeg')
web.header('Content-transfer-encoding','binary')
return open(filename, 'rb').read()
except:
return 'error 404: resultplot.jpg not found'
class downloadtxt:
def GET(self):
filename = 'usr/' + session.get("username") + '/config.txt'
print(filename)
try:
web.header('Content-Disposition', 'attachment; filename="config.txt"')
return open(filename).read()
except:
return 'error 404: config.txt not found'
class getusrparams:
def GET(self):
title = 'Input of simulation parameters'
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment