# Adding a file while preserving filename using js-ipfs

**URL:** <https://discuss.ipfs.tech/t/adding-a-file-while-preserving-filename-using-js-ipfs/2060>\
**Category:** Help\
**Tags:** js-ipfs\
**Created:** [February 11, 2018, 1:01am UTC](https://discuss.ipfs.tech/t/adding-a-file-while-preserving-filename-using-js-ipfs/2060 "2018-02-11T01:01:03Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![doraemondrian](https://avatars.discourse-cdn.com/v4/letter/d/8797f3/32.png) [@doraemondrian](https://discuss.ipfs.tech/u/doraemondrian)\
**Post date:** [February 11, 2018, 1:01am UTC](https://discuss.ipfs.tech/t/adding-a-file-while-preserving-filename-using-js-ipfs/2060/1 "2018-02-11T01:01:03Z")

</div>

Hi, I have a functional app that uses IPFS, it successfully takes a local file via `<input type='file'>`, turns it into stream, and adds it to IPFS through `node.files.add`. And it returns the hash, which I can use to retrieve the original file. So for example if I had an image, I could upload it and display it immediately as an `<img>` tag.

But here’s the problem I’m facing. I am trying to create a download link (`<a href>`) that links to the `ipfs.io` gateway so the file can be downloaded through a link. For example I want to upload a PDF file to IPFS and create a gateway link where the file can be downloaded.

But because the gateway url ends with the hash (and not a file extension), whenever I try to download it (or open it in my iOS safari in a new app), it’s not recognized. I’ve looked into all the github issues remotely close to solving this issue, but the closest I’ve found was [Add support for wrapping quick uploads in a directory. · Issue #349 · ipfs/ipfs-companion · GitHub](https://github.com/ipfs-shipyard/ipfs-companion/issues/349) which doesn’t seem to have a solution yet.

If we were just talking about regular web servers, I could just add a file type extension in the end and the browser will know what to do with it, but since all IPFS files (unless they are wrapped in a directory) end with a hash, this is not a feasible solution either.

I know one way to do it manually is to wrap the file in a directory by adding file in the console via `add -w` option, but couldn’t find a way to do this with JS.

Am I missing something? Or is there currently no way (using JS-IPFS) to upload a file to IPFS and then make it downloadable as a link?

---

<div class="post-metadata">

**Author:** ![lidel](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ipfs.tech/lidel/32/9853_2.png) [@lidel](https://discuss.ipfs.tech/u/lidel)\
**Post date:** [May 18, 2018, 6:56pm UTC](https://discuss.ipfs.tech/t/adding-a-file-while-preserving-filename-using-js-ipfs/2060/2 "2018-05-18T18:56:53Z")

</div>

If you want to preserve file name, you need to wrap it in a directory.

Until `wrapWithDirectory` flag lands in `js-ipfs` ([PR](https://github.com/ipfs/js-ipfs/pull/1329) is merged, but not released yet) you can do it manually by adding a named file link to an empty unixfs-dir via [`object.patch.addLink` API](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/OBJECT.md#objectpatchaddlink).

In command line, it looks like this:

```
ipfs object patch add-link <root_CID> <file_name> <file_CID> - Add a link to a given object

```

We know that `QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn` is the CIDv0 of an empty dir, so all you really need is one command:

```auto
$ ipfs object patch QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn add-link bug_or_feature.jpg Qmayz4F4UzqcAMitTzU4zCSckDofvxstDuj3y7ajsLLEVs

```

Example above returns `QmVnLkRxWWv2xiPKeQy6re1zCepzX7YXUiq5fESkd1j8bC` – a new CID representing a wrapping directory with one file:

[https://ipfs.io/ipfs/QmVnLkRxWWv2xiPKeQy6re1zCepzX7YXUiq5fESkd1j8bC/](https://ipfs.io/ipfs/QmVnLkRxWWv2xiPKeQy6re1zCepzX7YXUiq5fESkd1j8bC/)  
[https://ipfs.io/ipfs/QmVnLkRxWWv2xiPKeQy6re1zCepzX7YXUiq5fESkd1j8bC/bug\_or\_feature.jpg](https://ipfs.io/ipfs/QmVnLkRxWWv2xiPKeQy6re1zCepzX7YXUiq5fESkd1j8bC/bug_or_feature.jpg)

JS-IPFS API is [here](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/OBJECT.md#objectpatchaddlink). Hope this helps.
