Below is a snippet of my React Js code where I pass the SVG file to a smart contract. The problem is the SVG is blank when it is passed to the smart contract function.
Also, when I console log the SVG I get the following:
{name: ‘gorilla-head.svg’, lastModified: 1684910840362, lastModifiedDate: Wed May 24 2023 15:47:20 GMT+0900 (Japan Standard Time), webkitRelativePath: ”, size: 9720, …}
try {
console.log(svg);
console.log("Made it to the minting function");
//console.log(svg.name);
if (svg.toString() == "") {
console.log("svg file cannot be empty");
} else {
console.log(svg);
const response = await contract.mintNft(svg); //This is where the svg file is pass to the contract
console.log("response: ", response);
await response.wait(1);
console.log(
`Create SVG to NFT index 0 has tokenURI: ${await contract.tokenURI(
1
)}`
);
}
} catch (err) {
console.log("error: ", err);
}
I also display the SVG file to the user whenever it is uploaded using:
{svgFile && <img src={URL.createObjectURL(svg)} alt="SVG Preview" />}
2
Answers
I found the following solution:
When you console.log(svg) it will print out the text of the SVG and when I pass the data to the contract it performs correctly.
Hope this helps someone in the future.
Since SVG files are essentially XML-based text files, you can convert the SVG file into a string representation and then pass it to the smart contract function as a string parameter.
and then …