Node.js如何获取文件大小

JavaScript024

Node.js如何获取文件大小,第1张

const size = await utils.getFileSize(filePath)

getFileSize(file) {

    file = decodeURI(file)

    return new Promise((resolve) =>{

      if (!file) {

        resolve(0)

        return

      }

      const filePath = path.resolve(__dirname, "../public", file)

      fs.stat(filePath, (err, data) =>{

        if (err == null) {

          resolve(data.size)

          return

        }

        resolve(0)

      })

    })

  },

获取上传文件的大小:

<input type="file" name="fileId" id="fileId" class="height30 width-245">

function findSize(field_id){

var byteSize = 0

if (!+[1,] &&!$("#"+field_id).files) {

var filePath = $("#"+field_id).val()

var fileSystem = new ActiveXObject("Scripting.FileSystemObject")

byteSize = fileSystem.GetFile(filePath).size

} else{

var fileInput = $("#"+field_id)[0]

byteSize = fileInput.files[0].size

}

return ( Math.ceil(byteSize / 1024) )

}