Skip to content

Instantly share code, notes, and snippets.

@arefaslani
Created September 10, 2020 12:51
Show Gist options
  • Save arefaslani/1e907830c4d4528071c2bac19f941bea to your computer and use it in GitHub Desktop.
Save arefaslani/1e907830c4d4528071c2bac19f941bea 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