1

I'm currently working with slack and struggling to retrieve file data when listning for events. My issue is not exactly related to slack so experience with it isn't required to help me out (thanks in advance:) ).

I have a program that successfully listens for events of files being shared to my Slack channel, and retrieves all file data. This data includes a private URL and private url download link to access the file, but not the file itself. I need to use one of these links to access the file data and save the file as a base64 encoded string, and this is where I am struggling to find a solution. (Edit: I cannot save the file locally)

Anything that can read a link like this: https://files.slack.com/files-pri/T05BB0WBGCV-F05HU6Y2X1Q/asfhaslf.pdf and retireve the binary data or something similar would be the goal.

Edit: the slack URL's are private and don't seem to allow me to retrieve the file data using fetch, instead I get the whole HTML document, so now I'm trying to find another solution.

Thanks again

1 Answer 1

2

Use an ArrayBuffer:

The ArrayBuffer object is used to represent a generic raw binary data buffer.

This will store the data from the downloaded URL in memory. Then you can convert the buffer to a nodejs Buffer which has a .toString() function that accepts passing in 'base64' as a parameter to get a base64-encoded string.

Here's an example:

  const response = await fetch("https://your.url/here.jpg")
  const buffer = Buffer.from(await response.arrayBuffer())
  const base64text = buffer.toString('base64')
  const resAsBase64 = `data:${response.type};base64,${base64text}`
  console.log(resAsBase64)

Source

Sign up to request clarification or add additional context in comments.

8 Comments

thanks so much for the response. I'll add this to my question, but I can't actually save the files locally which is what makes this quite hard
@MeganH that makes sense! I'll update my answer with instructions for how to get the file as a Blob instead.
Thanks for the update and help! I'm still having a bit of trouble, I think the MDN example is actually creating a URL like the one I would like to access, instead of reading from it. So far I've been able to fetch data but not file data.
@MeganH does the data need to be base64 encoded, or does it just need to be a string of some sort? If you just need a string, doing await blob.text() would probably be sufficient.
This is late now, but that was exactly it! Thanks @chill389cc
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.