NodeJS - Upload docx using Formidable - Where does it upload?

Viewed 48

My objective is to upload a .docx from a client to Webserver/NodeJS/EC2 via Chrome using NodeJS, HTML & JavaScript & the .docx should be available in a specific directory on the NodeJS/Webserver/EC2.

Here's what I've done so far:

HTML Snippet

<body bgcolor=d9d9d9>
    <form action="https://kvyfcz.sse.codesandbox.io/upload" method="POST" id=form1 enctype="multipart/form-data">
        <h3>Please load the Doc File</h3>
        <b>Step 1
        <input type="file" id=docxFile accept=".doc,.docx,application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document" name="filetoupload" />
        <input type="submit" />
        <!-- <button type="submit" form="form1" value="Submit">Upload</button> -->
    </form>

NodeJS Part

http
    .createServer(function (req, res) {
        if (req.url === "/upload") {
        res.write("<h1>Dirname is {" + __dirname + "}<br>");
        var form = new formidable.IncomingForm();
        form.parse(req, function (err, fields, file) {
        var oldpath = file.filetoupload.filepath;
        var newpath = __dirname + file.filetoupload.originalFilename;
        fs.rename(oldpath, newpath, function (error) {
            if (error) res.write("Rename Error occurred!!");
            var output1 = "oldpath is {" + oldpath + "} & newpath is {" + newpath + "} & Dirname is {" + __dirname + "}";
            res.write(output1);
            fs.readFile(newpath, "utf-8", function (err, data) {
            if (err) res.write("FS Error occurred!!");
            else {
                res.write(data);
            }
        });
    });
});

Output

Dirname is {/sandbox} oldpath is {/tmp/83f13a67b2e8a684fd4ddbd01} & newpath is {/sandboxIronman - TestMe.docx} & Dirname is {/sandbox}PK!K���l[Content_Types].xml �(�̗

Issues: As such, formidable reads the .docx file, renames it successfully & the file content is displayed on Browser. However

  1. I'm under the impression that the .docx gets FTP'd into current directory from where the NodeJS is running, but it seems that's not the case. How can I accomplish this?
  2. In the NodeJS code, if I change the existing line from
var newpath = __dirname + file.filetoupload.originalFilename;

to

var newpath = file.filetoupload.originalFilename;

There're errors. How can I get the new filename to be Ironman - TestMe.docx instead of /sandboxIronman - TestMe.docx

Thanks in advance RG

1 Answers

Ok, this issue is resolved. Solution came from this post - nodejs formidable changing uploadDir

Changed the code as follows:

http.createServer(function (req, res) {
    if (req.url === "/upload") {
      var form = new formidable.IncomingForm({uploadDir: __dirname});
      form.parse(req, function (err, fields, file) {
        var oldpath = file.filetoupload.filepath;
        var newpath = file.filetoupload.originalFilename;
        fs.rename(oldpath, newpath, function (error) {
          if (error) res.write("File upload error occurred!!");
          else {
            var output1 = newpath + "{" + newpath + "}";
            res.write(output1);
          }
        });
      });
    });
});

Related