Skip to content

Lesson Feedback%3A Regular Files API - Lesson 4 (Read the contents of a file) #812

Open
@heaversm

Description

@heaversm

Have a question or suggestion regarding a specific ProtoSchool lesson? Please use this template to share it!

URL of the lesson that's confusing:

https://proto.school/regular-files-api/04

What's confusing about this lesson?

In your example, calling toString() returns a readable string. In my own prototype - uploading a plaintext .txt file and then trying to read that back from the CID path results in an array of numbers using the same code:

const ipfsID = { path: 'QmUUSLeERmuFa63g6aFx7niouQyRUbHq81VXBgiCadPVZt' };
const bufferedContents = await toBuffer(ipfs.cat(ipfsID.path));
console.log(bufferedContents); //ouputs "Uint8Array(57)[[ 76, 105, 110, 107, 101, 100, 73, 110, 32, 45...]"
const stringContents = bufferedContents.toString();
console.log(stringContents); //outputs "76, 105, 110, 107, 101, 100, 73, 110, 32, 45..."

I realized this is because an uploaded plaintext file (say, via a file upload box) doesn't get stored as text, it gets stored as a file, so to read its contents you have to do something like

const fileURL = `https://ipfs.io/ipfs/${ipfsID.path}`;

fetch(fileURL)
.then( data => data.text() )
.then( fileContents => {
  setFileContents(fileContents);
  setImgURL(null);

What additional context could we provide to help you succeed?

how to read the contents of an uploaded .txt file to a string from an IPFS CID Path

What other feedback would you like to share about ProtoSchool?

Pretty good! Enjoying it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions