Reputation: 992
glowscript.org is a Python-based GAE that stores user-written scripts in a datastore. How would I program in the Python server and/or JavaScript client a button that lets the user download to the user's Download directory one or more of the user's scripts? All I was able to find in my searching seemed to be oriented to me downloading the datastore from a command line, which seems unrelated to my case.
Upvotes: 0
Views: 58
Reputation: 5276
Edit: I modified my csv example to download a python file (.py
) instead
My Datastore model
class MetricsJob(ndb.Model):
result = ndb.TextProperty(compressed=True) # the file body
name = ndb.StringProperty()
# ... other fields
My Request handler
class MetricsDownload(webapp2.RequestHandler):
def get(self, key):
obj = ndb.Key(urlsafe=key).get()
self.response.headers['Content-disposition'] = 'attachment; filename='+obj.name+'.py'
self.response.headers['Content-Transfer-Encoding'] = 'Binary'
self.response.headers['Content-Type'] = 'text/plain'
self.response.out.write(obj.result)
HTML code, all you need to do is initiate a HTTP GET:
<a target="_blank" href="/metrics/download/ahFkZXZ-cGF5LXRvbGxvLXdlYnIRCxIEVXNlchiAgICAgODECww/"></a>
The key to getting the browser to treat the HTTP GET as a file download was the response headers.
For your use case I would imagine you would need to set:
self.response.headers['Content-Type'] = 'text/plain'
I'm not sure about the others
Upvotes: 1