0

I made a research and found only forms and formidable tutorials. However, I couldn't find how to get the original file name.

I am using Postman to send a file to http://localhost:8081/file. The file is sent in binary as body. The file sent is xxx.json.

In Node I created an HTTP server:

const http = require("http");
const fs = require("fs");

const server = http.createServer(async (req, res) => {

  if (req.method === "POST" && req.url === "/file") {
    req.on("data", (chunk) => {
      console.log(`Data chunk available: ${chunk}`);
      //   fs.createWriteStream("./finalFolder");
    });
  }

  res.end();
});

I want to save the file to /finalFolder preserving the original filename xxx.json. Where do I get the name of the file uploaded?

Ken White
  • 123,280
  • 14
  • 225
  • 444
Pikk
  • 2,343
  • 6
  • 25
  • 41
  • if you'll pardon the question, why use plain node.js for this, rather than an actual, proper web server (like express) that you can load all the bits you need for, in order to make the things you want to do easy, rather than low-level hard? – Mike 'Pomax' Kamermans Mar 16 '22 at 23:56
  • Legit question... because I am learning first plain.node. Express will be in a future module of the course. Step by step – Pikk Mar 17 '22 at 08:00
  • ah, then it'll be worth pointing out that this is basically coursework. because you wouldn't normally use plain node for this at all, as node's http and https modules are super barebones, purely giving you a way to handle messages that use the hypertext transfer protocol. So anything data you need is either in the HTTP request headers, or the HTTP body (if there is one). In this case, the filename will be in the value for (one of) the Content-Disposition headers. See https://stackoverflow.com/a/8660740/740553 – Mike 'Pomax' Kamermans Mar 17 '22 at 16:03

0 Answers0