This is in my index.ejs
<script src="https://cdn.jsdelivr.net/npm/ipfs-core/dist/index.min.js"></script>
<script>
async function saveFile(file) {
const node = window.IpfsCore.create();
return await node.files.add(file);
}
function receivedText() {
document.getElementById('ipfs').appendChild(document.createTextNode(fr.result));
}
async function readfichier(e) {
if(window.FileReader) {
var file = e.target.files[0];
var reader = new FileReader();
if (file && file.type.match('image.*')) {
reader.readAsDataURL(file);
}
reader.onloadend = function (e) {
}
console.log(file);
document.getElementById('fileNameOutput').innerHTML = document.getElementById('fileName').value;
document.getElementById('ipfs').innerHTML = await saveFile(JSON.stringify(file));
}
}
function clickme() {
$('file').ready(function(){ const file = $('file').prop('files')});
var fr = new FileReader();
fr.onload = select;
document.getElementById('file').addEventListener('change', readfichier, false);
//fr.readAsText(file);
//fr.readAsBinaryString(file); //as bit work with base64 for example upload to server
}
function select() {
var id = $('file').prop('files');
$.getJSON("/upload?" + $.param(id), function (res) {
document.getElementById('ipfs').innerHTML = res;
});
}
</script>
And this is in my index.js
app.get('/upload', async (req,res) => {
const file = req.files.file;
const fileHash = await saveFile(JSON.stringify(file));
res.send(fileHash)
})
function success(result){
return result
}
async function saveFile(file) {
await ipfs.add(file, (err, result) => {
if (err){
console.log(err);
}
console.log(result);
});
}
I'm trying to upload a file and store it on the IPFS blockchain, and then load the hash of that file into the front-end.
What this should enable me to do is to load PDFs and Videos and Music and Images, and store them all on the blockchain and then implement them all onsite.