Rudy
Rudy

Reputation: 7044

Preserve the modified date of a file during download

I got feedback to preserve the modified date of downloaded file. I found a way to preserve it if I serve the file inside a zip, however got problem when I just serve the file as it is from my nodejs server.

Below is my current implementation :

  try{
    var stat = fs.statSync(fullpath);
    self.response.writeHead(200, {
         'Content-Type': mimeType,
         'Last-Modified': stat.mtime // not working
    });
    var fileStream = fs.createReadStream(fullpath);
    fileStream.pipe(self.response);
    fileStream.on('end', function() {
      console.log("complete")
    });
  }catch(e)
  { //to handle user cancel the download and bring down whole system
    logger.error("streaming failed,because of:"+e.message);
  }

Initially I thought that setting the header 'Last-Modified' should do the trick but apparently it is not. Need to be able to work in Chrome, but if it works across browser, it would be great.

Note : It is not because of the format because using "Tue, 15 Nov 1994 12:45:26 GMT" instead of stat.mtime is not working as well.

Update : Seems impossible for browser right now as per early 2017, as shown in this link, the only way to do this is curl, or wget.

Upvotes: 1

Views: 1765

Answers (1)

orev
orev

Reputation: 186

Do you mean the modified date on a file when the browser has downloaded and saved it? You can't do that, as that would require operating system access on the remote computer. The modified date on a file is a function of the filesystem on the client computer.

Upvotes: 1

Related Questions