Last active
November 21, 2021 03:58
-
-
Save arifvn/d654b6b79a14aba3bc35f3338a58fa97 to your computer and use it in GitHub Desktop.
Sample read and upload file using axios
This file contains 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
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