The iwrite() method accepts the location of a file in the local server storage as an input parameter. It then adds this file to the IPFS network and returns the hash of the document to the function invoker.
The method receives the path of the file in the filepath input parameter, as shown in the following code snippet:
async function iwrite (filepath) {
Next, it adds the file to IPFS by calling the ipfs client object. The client object submits the file to the IPFS client port 5001, as follows:
try
{
const results = await ipfs.addFromFs(filepath, { });
The IPFS client returns the hash of the file content. The iwrite() method returns this file hash to the invoker, like this:
return results;
}catch (error) {
console.error(`Failed to write: ${error}`);
}
}
A catch statement catches any errors while adding the file to the IPFS network.
That brings us to the end of the iwrite() method. Next, let's look at the method that submits the transaction to the blockchain network.