Upload a file into ipfs and retrieve the CID using JS:

But when i go to ipfs.io/ipfs/CID, it want me to go through the whole directory to get to the uploaded file: the code is:-

const IPFS = require("ipfs");

async function main() {
  const node = await IPFS.create();
  const version = await node.version();

  console.log("Version:", version.version);

  const fileAdded = await node.add({
    path: "FILE_PATH",
    content: "DESCRIPTION",
  });

  console.log("Added file:", fileAdded.path, fileAdded.cid);

}

main();

this code gives me the CID of my uploaded file, so why does it giving me whole directory when i want to look up my file on browser,
for example if file path is :: home/A/B/1.png
then when i go to ipfs.io/ipfs/CID,
it takes me to ipfs, and there is a folder home,
when i go in home, there is a folder A then B and then 1.png and after clicking on 1.png, I can see my file. But i want to look up to my file directly.

IPFS always returns the root of all files when you add by a folder like that. Your actual file should be available at something address like [CID]/A/B/1.png

oh, but iā€™m actually unable to view my file that i uploaded,
like this is the cid ipfs.io/ipfs/QmVhKKnojNWPa5Lza3qnVE5PJTgaBWjTAMS8JpsgZane9L of a file that i uploaded, but my file(image file that i uploaded) is not visible there after goin to [CID]/A/B/1.png

The data in that IPFS file is 5 bytes of text: ā€œImageā€. :slight_smile: I think either you accidentally have a text file named ā€œ3.pngā€, or more likely issue is a typo on your upload code causing it to stream the text ā€œImageā€ instead of the actual file data.

itā€™s like to add content along with path, so iā€™m unable to make the changes in code as iā€™m uploading an imageā€¦