Skip to content

Instantly share code, notes, and snippets.

@arifvn
Last active November 21, 2021 03:58
Show Gist options
  • Save arifvn/d654b6b79a14aba3bc35f3338a58fa97 to your computer and use it in GitHub Desktop.
Save arifvn/d654b6b79a14aba3bc35f3338a58fa97 to your computer and use it in GitHub Desktop.
Sample read and upload file using axios
var axios = require('axios');
var fs = require('fs');
var path = require('path')
var util = require('util')
let readfile = util.promisify(fs.readFile)
async function sendData(url,data) {
let params = data
let resp = await axios({
method: 'post',
url: url,
data: JSON.stringify(params),
headers: { 'Accept': 'application/json', 'Content-Type': 'application/json' }
maxContentLength: Infinity,
maxBodyLength: Infinity
}).catch(err => {
throw err;
})
return resp;
}
async function ReadFile(filepath) {
try{
let res = await readfile(filepath,'base64')
let filename = path.basename(filepath).split('.').slice(0, -1).join('.')
let ext = path.extname(filepath)
return {data:res,fext:ext,fname:filename}
let x = 1
}
catch(err)
{
throw err
}
}
(async () => {
try {
let img = await ReadFile('Files/1.pdf')
let res = await sendData('http://localhost:3000',img)
console.log(res)
}
catch (ex) {
console.log(ex)
}
}
)();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment