richardgirges / express-fileupload

Simple express file upload middleware that wraps around busboy
MIT License
1.52k stars 261 forks source link

Get the last modified date of the uploaded file?(ctime, mtime, atime) #248

Closed digster closed 2 years ago

digster commented 3 years ago

I need the last modified date of the uploaded file. But on inspecting I can see that the dates are reset to the current date and time. Is there a way where we can access the uploaded file's real modified and access dates?

BCsabaEngine commented 3 years ago

When you upload a file you get a copy (data only + filename) of original file. You cannot reference to original one. I think it is not possible.

gjf7 commented 2 years ago

Like @BCsabaEngine said above, what you can do is to send last modified date with file to server together.

Here is a example:

client:

<html>
    <body>
        <input id="files-list" type="file">
    <body>
    <script>
        const fileList = document.getElementById("files-list")
        fileList.addEventListener("change", (e) => {
            const files = e.target.files
            let i = 0, len = files.length
            while(i < len){
                request(files[i++])
            }
        })
        function request(file){
            const xhr = new XMLHttpRequest()
            const formData = new FormData()
            formData.append("lastModified", file.lastModified)
            formData.append("file", file)
            xhr.open("post", "http://localhost:8000/upload", true)
            xhr.send(formData)
        }
    </script>
</html>

server:

const express = require("express")
const cors = require("cors")
const path = require("path")
const fileupload = require("./fileupload")
const app = express()

app.use(cors())
app.use(fileupload({
  useTempFiles: true,
}))

app.post("/upload", (req, res) => {
  const name = req.files.file.name
  console.log(req.body.lastModified)  // Get lastModified timestamp here
  req.files.file.mv(path.join(__dirname, "/img/") + name)
    .then(() => {
      res.writeHead(200, { 'Content-Type': 'application/json' })
      res.end(JSON.stringify({ message: "upload successfully" }))
    })
})

app.listen(8000, () => {
  console.log("app online")
})