Skip to content

Instantly share code, notes, and snippets.

@alfianokt
Created March 21, 2021 03:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alfianokt/f63b69ee5a1bd7b0a33e244a79c769e6 to your computer and use it in GitHub Desktop.
Save alfianokt/f63b69ee5a1bd7b0a33e244a79c769e6 to your computer and use it in GitHub Desktop.
Nodejs POST file from local
/**
* Upload local image to telegraph using nodejs
*
* @alfianokt
* 20-03-2021
*
*/
const fs = require('fs');
const axios = require('axios').default;
const FormData = require('form-data');
const filePath = __dirname + '/photo.png';
// async
//
// fs.readFile(filePath, (err, file) => {
// if (err) {
// throw err;
// }
// const body = new FormData();
// body.append('file', file, {
// filepath: filePath,
// contentType: 'image/png;image/jpeg'
// });
//
// axios.post('https://telegra.ph/upload', body, {
// headers: body.getHeaders(),
// })
// .then(r => {
// console.log(r);
// })
// .catch(e => console.error(e));
// });
// sync
//
const body = new FormData();
body.append('file', fs.readFileSync(filePath), {
filepath: filePath,
contentType: 'image/png;image/jpeg'
});
axios.post('https://telegra.ph/upload', body, {
headers: body.getHeaders(),
})
.then(r => {
console.log(r);
})
.catch(e => console.error(e));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment