Check if a file exists locally using JavaScript only

后端 未结 10 1709
盖世英雄少女心
盖世英雄少女心 2020-12-03 14:01

I want to check if a file exists locally, where the HTML file is located. It has to be JavaScript. JavaScript will never be disabled. jQuery is not good but can do.

10条回答
  •  心在旅途
    2020-12-03 14:37

    No need for an external library if you use Nodejs all you need to do is import the file system module. feel free to edit the code below: const fs = require('fs')

    const path = './file.txt'
    
    fs.access(path, fs.F_OK, (err) => {
      if (err) {
        console.error(err)
        return
      }
    
      //file exists
    })
    

提交回复
热议问题