Last active
February 2, 2019 02:51
-
-
Save shankarnakai/b6cbb949b3e6c2589bb83150e179fa6e to your computer and use it in GitHub Desktop.
NodeJs Post multipart/form-data
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
//#Request Library | |
//##EXAMPLE | |
const request = require('request') | |
const formData = { | |
fileUploaded: { | |
value: fileContent,//it should contain your pdf binary data | |
options: { | |
filename: 'FILE_NAME.pdf', | |
filepath: 'FILE_NAME.pdf', | |
contentType: 'application/pdf', | |
} | |
} | |
} | |
const req = request.post({ | |
url: url, | |
formData: formData | |
}, (err, httpResponse, body) => { | |
if (err) { | |
console.error('Unexpected error:', err.message) } | |
} | |
console.log('Success') | |
}) | |
//#PRATICAL EXAMPLE | |
//#SERVER | |
var express = require('express'); //Express Web Server | |
var bodyParser = require('body-parser'); //connects bodyParsing middleware | |
var formidable = require('formidable'); | |
var path = require('path'); //used for file path | |
var fs = require('fs-extra'); //File System-needed for renaming file etc | |
var app = express(); | |
app.use(express.static(path.join(__dirname, 'public'))); | |
/* ========================================================== | |
bodyParser() required to allow Express to see the uploaded files | |
============================================================ */ | |
app.use(bodyParser({ | |
defer: true | |
})); | |
app.route('/upload') | |
.post(function(req, res, next) { | |
var form = new formidable.IncomingForm(); | |
//Formidable uploads to operating systems tmp dir by default | |
form.uploadDir = "./img"; //set upload directory | |
form.keepExtensions = true; //keep file extension | |
form.parse(req, function(err, fields, files) { | |
res.writeHead(200, { | |
'content-type': 'text/plain' | |
}); | |
res.write('received upload:\n\n'); | |
fs.rename(files.fileUploaded.path, './img/' + files.fileUploaded.name, function(err) { | |
if (err) | |
throw err; | |
console.log('renamed complete'); | |
}); | |
res.end(); | |
}); | |
}); | |
var server = app.listen(3030, function() { | |
console.log('Listening on port %d', server.address().port); | |
}); | |
//SENDER.js | |
const request = require('request') | |
const fs = require('fs') | |
fs.readFile('file.pdf', async (err, content) => { | |
if (err) { | |
return console.log('Unexpected error' + err.message) | |
} | |
try { | |
const url = 'http://localhost:3030/upload' | |
const success = await uploadPDF(url, content) | |
if (success) { | |
console.log('File sent with success') | |
} | |
} catch (err) { | |
console.error('Unexpected error:', err) | |
} | |
}); | |
function uploadPDF(url, fileContent) { | |
return new Promise((resolve, reject) => { | |
const formData = { | |
fileUploaded: { | |
value: fileContent, | |
options: { | |
filename: 'file.pdf', | |
filepath: 'file.pdf', | |
contentType: 'application/pdf', | |
} | |
} | |
} | |
const req = request.post({ | |
url: url, | |
formData: formData | |
}, (err, httpResponse, body) => { | |
if (err) { | |
return reject(err) | |
} | |
return resolve(true) | |
}) | |
return resolve(true) | |
}) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@filipenos take a look in this example here.