From 08b3b64f07eb00988db7c4241cdd44efe046b025 Mon Sep 17 00:00:00 2001 From: Khac Man Date: Wed, 2 Aug 2017 23:07:32 +0700 Subject: [PATCH] fs.read() expects at the 2nd argument causes error loading file from path. --- lib/multipartform.js | 31 +++++++++++-------------------- 1 file changed, 11 insertions(+), 20 deletions(-) diff --git a/lib/multipartform.js b/lib/multipartform.js index 7196724..769727d 100644 --- a/lib/multipartform.js +++ b/lib/multipartform.js @@ -1,5 +1,5 @@ var fs = require('fs'); -var sys = require('util') +var sys = require('util'); exports.defaultBoundary = '48940923NODERESLTER3890457293'; @@ -109,25 +109,16 @@ Part.prototype = { //Now write out the body of the Part if (this.value instanceof File) { - fs.open(this.value.path, "r", 0666, function (err, fd) { - if (err) throw err; - - var position = 0; - - (function reader () { - fs.read(fd, 1024 * 4, position, "binary", function (er, chunk) { - if (er) callback(err); - stream.write(chunk); - position += 1024 * 4; - if (chunk) reader(); - else { - stream.write("\r\n") - callback(); - fs.close(fd); - } - }); - })(); // reader() - }); + fs.readFile(this.value.path, (err, fileBuff) => { + "use strict"; + if (err) { + throw err; + } + + stream.write(fileBuff); + stream.write("\r\n"); + callback(); + }); } else if (this.value instanceof Data) { stream.write(this.value.data); stream.write("\r\n");