6

I'm working on this project using IPFS and I'm trying to create a website that allows users to upload files directly from their browser to IPFS. My goal was that the website would be a front-end website but whenever I add a file to IPFS and I check it's hash on https://gateway.ipfs.io/ipfs/hash-here nothing happens, which made me think that the files are probably not getting uploaded to IPFS because I'm not running it on my local machine. Is this correct?

const Buffer = require('safe-buffer').Buffer;

export default function uploadFiles(node, files) {

    let reader = new FileReader();

    reader.onloadend = () => {

        let byteData = reader.result.split('base64,')[1];
        let fileData = Buffer.from(byteData);

        node.files.add(fileData, (err, res) => {

            if (err) {
                throw err
            }

            let hash = res[0].hash
            console.log(hash);  ///////prints a hash that isnt visible on 
                                //gateway

            node.files.cat(hash, (err, res) => {
                if (err) {
                    throw err
                }
                let data = ''
                res.on('data', (d) => {
                    data = data + d
                })
                res.on('end', () => {
                    // console.log(data);
                    // console.log(atob(data));
                })
            })

        });

    }

    reader.readAsDataURL(files['0']);

};
ninesalt
  • 4,054
  • 5
  • 35
  • 75
  • hey, long time since but have you succeeded with the above? is it possible to upload a file to IPFS without incorporating a daemon running as a backend? – lephleg Dec 14 '20 at 19:35

1 Answers1

4

Are you running a js-ipfs node in your browser? Did you get the chance to look at the examples in the examples folder in js-ipfs repo? Url here: https://github.com/ipfs/js-ipfs/tree/master/examples

If you add a file to your node and the node is on, the IPFS gateway node will be able to find the content from your browser node.

David Dias
  • 1,792
  • 3
  • 16
  • 28
  • Yes I am running a node, but when I paste the hash in the URL I pasted, the page just loads forever and nothing happens. – ninesalt Aug 22 '17 at 16:23
  • Can you paste a snippet that replicates your issue? – David Dias Aug 23 '17 at 17:28
  • Added. The snippet handles a file upload from another component. – ninesalt Aug 24 '17 at 07:42
  • Has that node been started? Looks like you are using it in offline more. Try calling node.isOnline() and see what Boolean it returns. – David Dias Aug 24 '17 at 08:23
  • Yes it has. node.isOnline() is true.I wouldnt get a hash if it wasnt, right? – ninesalt Aug 24 '17 at 09:04
  • No, js-ipfs also works offline. That is actually quite strange. Does the example on the repo work for you https://github.com/ipfs/js-ipfs/tree/master/examples/exchange-files-in-browser ? – David Dias Aug 25 '17 at 09:10
  • I didnt try this one because I wanted to try js-ipfs without installing the daemon on my computer. – ninesalt Aug 26 '17 at 10:00
  • Part of the example is "browser to browser" and "browser to gateway" which doesn't require your own local node. It should provide you the insight you are looking for. – David Dias Aug 28 '17 at 10:01
  • Assuming I'm correctly understanding the examples, they depend on my browser session not terminating because it is the node. I have a feeling I'm using the wrong library for what I'm trying to do. I'm trying to upload files to other nodes so that if my node shuts down the files are still there. – ninesalt Aug 28 '17 at 15:48
  • Ah, I see. There is a misinterpretation of how IPFS works then. IPFS won't force any node to download content that they haven't requested. If you want your content to remain available in the network, either you ensure that there is a node running with it or you pay a "pinning service". One of the most interesting ones (and I'm totally biased here) is http://filecoin.io. Others that are already available are: https://pinbits.io/ and https://www.eternum.io/ – David Dias Aug 29 '17 at 08:37
  • Hi, I am running an IPFS node remotely and trying to upload the file from js-ipfs example to the remote IPFS node (replacing the localhost to ip address). I do not want to run a local node in my desktop, however the example works with the localhost not with the remote node. Any help would be greatly appreciated. – KayKay Nov 16 '18 at 17:28
  • Hi @KayKay, that's odd. Do you have your node in the browser running long enough that the file gets transferred? Can you see the file in an ipfs gateway? E.g. ipfs.io/ipfs/ – David Dias Nov 25 '18 at 21:49