问题描述
$(document).ready(function() {
const ipfs = window.IpfsApi({
host: 'ipfs.infura.io',
port: 5001,
protocol: 'https'
});
const Buffer = window.IpfsApi().Buffer;
const ContractAddress = "0xf21ac1844861a76754d7c3db29067cb0970fb8b5";
if (typeof web3 === "undefined") {
return showError("Please install MetaMask.");
}
const contract = web3.eth.contract(Config.getContractABI()).at(ContractAddress);
function uploadCertificate() {
//TODO: Write the Logic behind Uploading a Certificate to IPFS and adding the information to the Blockchain
let filesDiv = $("#certificateForUpload");
if (filesDiv[0].files.length === 0) {
return showError("Please select a file.");
}
let fileReader = new FileReader();
fileReader.onload = function() {
let fileBuffer = Buffer.from(fileReader.result);
ipfs.files.add(fileBuffer, (err, result) => {
if (err) {
return showError(err);
}
if (result) {
let ipfsHash = result[0].hash;
contract.addCertificate(ipfsHash, function(err, result) {
if (err) {
return showError("There was error with smart contract: $(err)")
}
showInfo("Certificate $(ipfsHash) was <b> successfully added.</b> to the Registry.")
})
}
});
};
fileReader.readAsArrayBuffer(filesDiv[0].files[0]);
}
标签: javascriptnode.jsajaxethereumipfs
解决方案
推荐阅读