Skip to content

Instantly share code, notes, and snippets.

@Tonyce
Forked from arefaslani/arweave-binary-upload.js
Created August 27, 2022 00:34
Show Gist options
  • Save Tonyce/e356df325cd7b7018118fae31492a35f to your computer and use it in GitHub Desktop.
Save Tonyce/e356df325cd7b7018118fae31492a35f to your computer and use it in GitHub Desktop.
const file = document.querySelector("input[type=file]").files[0];
const reader = new FileReader();
reader.readAsArrayBuffer(file);
reader.onload = () => {
const key = require("../arweave-keyfile.json");
const arweave = Arweave.init({
host: "arweave.net",
port: 443,
protocol: "https",
timeout: 20000,
logging: false,
});
arweave
.createTransaction({ data: Buffer.from(reader.result) }, key)
.then(async (transaction) => {
transaction.addTag("Content-Type", file.type);
await arweave.transactions.sign(transaction, key);
const uploader = await arweave.transactions.getUploader(transaction);
while (!uploader.isComplete) {
await uploader.uploadChunk();
console.log(`${uploader.pctComplete}% complete, ${uploader.uploadedChunks}/${uploader.totalChunks}`);
}
})
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment