This code snippet will show you how to download a single file from a HydroShare rsource using the HydroShare API. This could be a useful function in the case where you need to get a specific file from a resource in HydroShare. This feature works very smoothly without any glitches.
Our demonstration code here is comprised of two steps. Step 1 will query the resource and generate a list of files available therein. Step 2 will actually download a selected file from a resource.
To try this on your own, you can use the code snippet below, or download the full javascript file that is used in this demonstration
auth = HydroShareAuthBasic(username= username, password= password)
hs = HydroShare(auth=auth)
fname = title
fpath = hs.getResourceFile(resourcein, fname, destination= '/tmp')
response = HttpResponse( content_type='application/force-download')
response['Content-Disposition'] = 'attachment; filename=%s' % smart_str(fname)
response['X-Sendfile'] = smart_str('/tmp')
messages.error(request, "File downloaded succesfully")
if has_errors:
messages.error(request, "Please fix errors.")