Created
March 21, 2021 03:49
-
-
Save fnzip/f63b69ee5a1bd7b0a33e244a79c769e6 to your computer and use it in GitHub Desktop.
Nodejs POST file from local
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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