Skip to main content

Command Palette

Search for a command to run...

Upload file with nodejs

Updated
1 min readView as Markdown
Upload file with nodejs
H

I'am mobile developer

client:

upload.html

<h1>abc</h1>
<form
  id="uploadbanner"
  enctype="multipart/form-data"
  method="post"
  action="/create"
>
  <input id="fileupload" name="my-file" type="file" />
  <input type="submit" value="submit" id="submit" />
</form>

Server :

app.js

const express = require("express");
const multer  = require('multer');

const storage = multer.diskStorage({
  destination: function (req, file, cb) {
    cb(null, './uploads/photos')
  },
  filename: function (req, file, cb) {
    cb(null,file.originalname);
  }
});

//const upload = multer({ dest: 'uploads/' })
const upload = multer({ storage: storage })

const app = express();

// website
app.use(express.static('client'));

app.get("/", (req, res) => {
  res.send("home");
  console.log("abc");
});



// app.post("/create",(req, res) => {
//   res.send("Uploaded");
// });
app.post("/create",upload.single('my-file'), (req, res) => {
  console.log("upload file");
  console.log(req.body);
  return res.status(200).json("upload file thanh cong!");
});


const port = 8080;

app.listen(port, () => {
  console.log("listening on port " + port);
});

postman:

upload.html

end

More from this blog

Hung Son's blog

35 posts

Sơn!