Question

how do i force download a file

Posted January 20, 2020 1.4k views
DigitalOcean Spaces

we are setting up a website that hosts mp3 files on spaces. We want it in such way that if the visitor clicks download, the mp3 doesn’t have to open a player. We want instant download

how do we achieve this with spaces?

sample link
https://spacename.sfo2.digitaloceanspaces.com/filename.mp3

with dropbox i could easily add &dl=1

how does it work with spaces?

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
Submit an Answer
1 answer

Hello,

What you could do is modify the metadata of the file and set the Content-Disposition which indicates if a regular file should be displayed inline in the web browser or downloaded as an attachment. For more information you could take a look at the official documentation here:

https://www.digitalocean.com/docs/spaces/how-to/set-file-metadata/

Hope that this helps!
Regards,
Bobby

  • Hey,

    I’m using @aws-sdk/s3-presigned-post with node js to generate presigned urls that I use to upload images on the client side.

    I’m able to add metadata fields like content-type and x-amz-meta-help but for some reason not content-disposition.

    This is some of the code I use to generate the presigned url:

    const { url, fields } = await createPresignedPost(client, {
      Bucket: bucketName,
      Key: fileName,
      Fields: {
        "content-type": "image/png",
        "x-amz-meta-help": "No",
        "content-disposition": "attachment",
        acl: "public-read",
      },
    });
    

    And after I upload an image using the generated url and check the uploaded image in the Digitalocean spaces I can see that it correctly added the Content-Type and x-amz-meta-help metadata fields but not the Content-Disposition.

    Is there any way to set content-disposition metadata without manually changing it through the ui?

    Thanks,

    Austin