Skip to content

Instantly share code, notes, and snippets.

@sogoiii
Created July 18, 2017 21:07
Show Gist options
  • Save sogoiii/e07ff464c4ff8a6fa9daa0ca927af3cb to your computer and use it in GitHub Desktop.
Save sogoiii/e07ff464c4ff8a6fa9daa0ca927af3cb to your computer and use it in GitHub Desktop.
Browser to IPFS example
<!DOCTYPE html>
<html>
<head>
<title>JavaScript file upload</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<script src="https://wzrd.in/standalone/buffer"></script>
<script src="https://unpkg.com/[email protected]/dist/index.js"
integrity="sha384-5bXRcW9kyxxnSMbOoHzraqa7Z0PQWIao+cgeg327zit1hz5LZCEbIMx/LWKPReuB"
crossorigin="anonymous"></script>
</head>
<script type="text/javascript">
function upload() {
const reader = new FileReader();
reader.onloadend = function() {
const ipfs = window.IpfsApi('localhost', 5001) // Connect to IPFS
const buf = buffer.Buffer(reader.result) // Convert data into buffer
ipfs.files.add(buf, (err, result) => { // Upload buffer to IPFS
if(err) {
console.error(err)
return
}
let url = `https://ipfs.io/ipfs/${result[0].hash}`
console.log(`Url --> ${url}`)
document.getElementById("url").innerHTML= url
document.getElementById("url").href= url
document.getElementById("output").src = url
})
}
const photo = document.getElementById("photo");
reader.readAsArrayBuffer(photo.files[0]); // Read Provided File
}
</script>
<body>
<form action="/">
<fieldset>
<legend>Upload photo</legend>
<input type="file" name="photo" id="photo">
<button type="button" onclick="upload()">Upload</button>
</fieldset>
</form>
</br>
</br>
<a id="url"></a>
</br>
</br>
<img id="output">
</body>
</html>
@yashwanth2804
Copy link

how to read from url of ipfs and convert to buffer

@younasm
Copy link

younasm commented Jun 6, 2022

I am using the same code in my Ethereum project it was working fine but now it's giving me the following error. what should i do to make it work again.

Error: /api/v0/object has been removed, use /api/v0/dag instead

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment