Create file based on buffer data in nodejs


I am sending from front end client side a file, on the server side I have something like this:

{ name: 'CV-FILIPECOSTA.pdf', data: <Buffer 25 50 44 46 2d 31 2e 35 0d 25 e2 e3 cf d3 0d 0a 31 20 30 20 6f 62 6a 0d 3c 3c 2f 4d 65 74 61 64 61 74 61 20 32 20 30 20 52 2f 4f 43 50 72 6f 70 65 72 ... >, encoding: '7bit', mimetype: 'application/pdf', mv: [Function: mv] }

What I need is to create the file may be based on that buffer that is there, how can I do it?

I already searched a lot and didn't find any solution.

This is what I tried so far:

router.post('/upload', function(req, res, next) { if(!req.files) { return res.status(400).send("No Files were uploaded"); } var curriculum = req.files.curriculum; console.log(curriculum); curriculum.mv('../files/' + curriculum.name, function(err) { if (err){ return res.status(500).send(err); } res.send('File uploaded!'); }); });

You could use Buffer available with NodeJS:

let buf = Buffer.from('this is a test'); // buf equals <Buffer 74 68 69 73 20 69 73 20 61 20 74 65 73 74> let str = Buffer.from(buf).toString(); // Gives back "this is a test"

Encoding could also be specified in the overloaded from method.

const buf2 = Buffer.from('7468697320697320612074c3a97374', 'hex'); // This tells that the first argument is encoded as a hexadecimal string let str = buf2.toString(); // Gives back the readable english string // which resolves to "this is a tést"

After you have data available in the readable format, you could store it using the fs module in NodeJS.

fs.writeFile('myFile.txt', "the contents of the file", (err) => { if(!err) console.log('Data written'); });

So, after converting the buffered input to string, you need to pass the string into the writeFile method. You could check the documentation of the fs module. It will help you better understand things.



  • how to implement OnClickListener for custom ViewHolder in my recycler view?
  • Hosting existing node.js apps on Windows Azure
  • Google Cloud Dataflow consume external source
  • How to add GeoJSON(MultiLineString) layer to a Google Map
  • Is there a way to continuously pipe data from Azure Blob into BigQuery?
  • Do not save model on duplicate file - Django 2
  • How to store model in `.pb` file with Estimator in TensorFlow?
  • Images tile on Google map in android
  • Can the use of C++11's 'auto' deteriorate performance or even break the code?
  • How to share functions between components in angular?
  • How can I access the Google account user_id?
  • iOS: Address Book
  • How to delete first 7 characters of folder name by using batch script?
  • ROR + MVC Disable Browser Cache
  • Complex multiple if statements
  • Do iTunesConnect external testers get updates for *every* uploaded ipa file after Beta Approval?
  • Unable to connect to Azure MySQL Database through Azure Function - C#
  • JQuery Mobile Ajax Navigation in Single-Page Template
  • How to add html image in to velocity template file to send email?
  • Regex not working in java 1.5
  • How to merge objects within array based on attribute
  • concise way of flattening multiindex columns
  • Is there a better way for handling SpatialPolygons that cross the antimeridian (date line)?
  • Conflicting declaration using constexpr and auto in C++11
  • How to call jQuery function in HTML returned by AJAX
  • How to specify generic type when the type is only known at runtime?
  • How do I add a mouse over tooltip to an Image using .DrawImage()
  • `$http:badreq Bad Request Configuration` - from angular post method, what is wrong here?
  • How to warp text around image in iOS?
  • Did not understand process of initialize in swift programming
  • How to encrypt Connectionstring written in web.config from codebehind?
  • Make checkout phone field optional for specific countries in WooCommerce
  • calling IO Operations from thread in ruby c extension will cause ruby to hang
  • Angular 4: Responsive Grid List
  • Firebase: How to read from external DB?
  • WPF custom control and direct content support
  • Write to .csv file with PHP (Commas in Data Error)
  • Create/delete users from text file using Bash script
  • Time Complexity of Fibonacci Algorithm [duplicate]
  • How to check if object is null in Java?