电子:从客户端获取文件夹结构

Electron: Get folder structure from client side

我正在开发 Angular2+Electron 桌面应用程序。作为样板,我举了一个简单的例子 Angular2 + Electron

我需要通过拖放文件夹到该区域来获取文件夹结构。我找到了如何在文件夹下降到区域后获取有关文件夹的信息的方法。现在需要借助此信息获取文件夹结构。这是返回对象的示例:

{
  lastModified: 1460044264000,
  lastModifiedDate: "Thu Apr 07 2016 18: 51: 04 GMT + 0300(EEST)",
  name: "dna",
  path: "/Users/myUser/Pictures/folder",
  size: 340,
  type: "",
  webkitRelativePath: ""
}

如能提供有关此问题的任何信息,我们将不胜感激!

首先使用fs stats检查是否是一个目录。

然后使用fs node module读取文件夹中的可用文件和文件夹。

更新

如果您使用 webpack 作为构建器,请务必设置 target: "electron-renderer" as mentioned in webpack docs

TS 会在 import * as fs from 'fs' 投诉。

解决此问题的第一种方法 是添加 declare var require:any 并使用 const fs = require('fs') - 丑陋的技巧。

其次是添加节点类型typings install dt~node -GS,配置tsconfig.json为:

{
  "compilerOptions": {
    "target": "ES6",
    "module": "commonjs",
    "removeComments": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "sourceMap": true
  },
  "exclude": [
    "node_modules"
  ],
  "filesGlob": [
    "./src/customDefinitions.d.ts",
    "./src/app/**/*.ts",
    "!./node_modules/**/*.ts",
    "typings/index.d.ts"
  ]
}

并使用import * as fs from 'fs';