# How to get file content

**URL:** <https://discuss.ipfs.tech/t/how-to-get-file-content/11458>\
**Category:** Uncategorized\
**Created:** [June 3, 2021, 1:31pm UTC](https://discuss.ipfs.tech/t/how-to-get-file-content/11458 "2021-06-03T13:31:29Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![novaknole](https://avatars.discourse-cdn.com/v4/letter/n/4da419/32.png) [@novaknole](https://discuss.ipfs.tech/u/novaknole)\
**Post date:** [June 3, 2021, 1:31pm UTC](https://discuss.ipfs.tech/t/how-to-get-file-content/11458/1 "2021-06-03T13:31:29Z")

</div>

I am using this [js-ipfs/FILES.md at 3d68a4493d7dcc550a4c449490f5c950d00c27fc · ipfs/js-ipfs · GitHub](https://github.com/ipfs/js-ipfs/blob/3d68a4493d7dcc550a4c449490f5c950d00c27fc/docs/core-api/FILES.md#example-3) with `ipfs.get`

Content is an Uint8Array. How do I get a file from it ? the idea is that sometimes hash of ipfs could have `json` file, sometimes `png` and so on. How is it possible to get the actual file ? I know that i can directly use `fetch(https://gateway.com/ipfs/cidHere`) but, this is a little bit worse, because gateways are usually slow… So I was thinking that `ipfs.get` was really way faster, but the content it returns is just uint8array and can’t really get the file out of it.

What would you suggest ?

---

<div class="post-metadata">

**Author:** ![stebalien](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ipfs.tech/stebalien/32/156_2.png) [@stebalien](https://discuss.ipfs.tech/u/stebalien)\
**Post date:** [June 3, 2021, 4:08pm UTC](https://discuss.ipfs.tech/t/how-to-get-file-content/11458/2 "2021-06-03T16:08:27Z")

</div>

That _is_ the file. Files are arrays of bytes.

---

<div class="post-metadata">

**Author:** ![stebalien](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ipfs.tech/stebalien/32/156_2.png) [@stebalien](https://discuss.ipfs.tech/u/stebalien)\
**Post date:** [June 3, 2021, 4:10pm UTC](https://discuss.ipfs.tech/t/how-to-get-file-content/11458/3 "2021-06-03T16:10:20Z")

</div>

You’re probably looking for [JavaScript: Create and save file - Stack Overflow](https://stackoverflow.com/questions/13405129/javascript-create-and-save-file)

---

<div class="post-metadata">

**Author:** ![novaknole](https://avatars.discourse-cdn.com/v4/letter/n/4da419/32.png) [@novaknole](https://discuss.ipfs.tech/u/novaknole)\
**Post date:** [June 3, 2021, 4:15pm UTC](https://discuss.ipfs.tech/t/how-to-get-file-content/11458/4 "2021-06-03T16:15:06Z")

</div>

@stebalien

Q1. I need to get file’s content without downloading it, because it’s a `metadata.json` which contains some key: values I need to extract. Do you think this is still possible ? but for sure, this goes out of IPFS topic totally.

Q2. Do you think `ipfs.get` is much faster than getting the file from gateway ? in the end, `ipfs.get` still makes a get request to the node… But in my opinion, `ipfs.get` doesn’t make a request to gateway, but directly to a node and this is why I think it’s much faster than gateway… Do I sound right ?

Thanks a lot.

---

<div class="post-metadata">

**Author:** ![stebalien](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ipfs.tech/stebalien/32/156_2.png) [@stebalien](https://discuss.ipfs.tech/u/stebalien)\
**Post date:** [June 3, 2021, 4:40pm UTC](https://discuss.ipfs.tech/t/how-to-get-file-content/11458/5 "2021-06-03T16:40:08Z")

</div>

> Q1. I need to get file’s content without downloading it, because it’s a `metadata.json` which contains some key: values I need to extract. Do you think this is still possible ? but for sure, this goes out of IPFS topic totally.

Yes. You need to convert the bytes to a string ([casting - Converting byte array to string in javascript - Stack Overflow](https://stackoverflow.com/a/51772442)), then parse it as JSON.

> Q2. Do you think `ipfs.get` is much faster than getting the file from gateway ? in the end, `ipfs.get` still makes a get request to the node… But in my opinion, `ipfs.get` doesn’t make a request to gateway, but directly to a node and this is why I think it’s much faster than gateway… Do I sound right ?

`ipfs.get` is the _right_ way to download files as it actually uses IPFS. The gateway exists as a crutch until IPFS is widely deployed and supported.

In terms of performance… it depends. If the gateway is under load, it might be slower. However, once the gateway finds/downloads the file once, it’ll likely be faster.

---

<div class="post-metadata">

**Author:** ![novaknole](https://avatars.discourse-cdn.com/v4/letter/n/4da419/32.png) [@novaknole](https://discuss.ipfs.tech/u/novaknole)\
**Post date:** [June 3, 2021, 4:42pm UTC](https://discuss.ipfs.tech/t/how-to-get-file-content/11458/6 "2021-06-03T16:42:47Z")

</div>

@stebalien

About the first one… It’s really hard to be doing this…

Sometimes, I get the direct hash which i can’t figure out what type it is, because `ipfs.get` doesn’t return its type at all which means I can’t download the file or anything like this… Sometimes it’s png, jpg or anything else and sometimes, it’s `.json` which means I need to decode it as you suggested above, but not sure about anything else as i can’t figure out about file type…

---

<div class="post-metadata">

**Author:** ![stebalien](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ipfs.tech/stebalien/32/156_2.png) [@stebalien](https://discuss.ipfs.tech/u/stebalien)\
**Post date:** [June 3, 2021, 5:01pm UTC](https://discuss.ipfs.tech/t/how-to-get-file-content/11458/7 "2021-06-03T17:01:31Z")

</div>

You either need to:

1. Get the filename. This is a part of the _directory_ not the file itself. If someone just gives you a hash of a file, you won’t have this information.
2. Detect the file type based on magic numbers. For that you’ll have to google “detect file type javascript”.
