Download a file from a resource

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.

Step 1: Get a list of files in a resource from HydroShare:

In the boxes below you enter your HydroShare username and its password. If you have to make an HydroShare account, click here.

The resource Id is a unique identifier for every resource on Hydroshare. You can findit by browsing to your HydroShare resource copying the ID from the URL. Like for example, this is an example hyperlink https://www.hydroshare.org/resource/08c6e88adaa647cd9bb28e5d619178e0/ from which the 32 letter combination at the end is called the resource id. And so the resource id from the example hyperlink is 08c6e88adaa647cd9bb28e5d619178e0

Resource ID

Step 2: Choose a file in the resource and delete it:

Learn more:

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.")