【发布时间】:2020-02-06 04:19:02
【问题描述】:
TypeError: 对象不支持属性或方法'readFile'
我刚刚创建了一个全新的 React 应用程序,但这正在发生...... 我不知道我还能做什么。
import * as React from "react";
import * as fs from "fs";
import * as zlib from 'zlib';
constructor(props: IReactComponentProps, state: IReactComponentState) {
super(props, state);
this.jsonPath = "/myfolder/mygzipedjsonfile.json.gz";
this.state = {
loading: true,
error: false,
};
this.test();
}
private test() {
fs.readFile(this.jsonPath, function (err, buffer) {
console.log('success');
});
}
错误:TypeError:对象不支持属性或方法“readFile”
ReactComponent.prototype.test
C:/Source/new-teste/src/ReactComponent.tsx:44
41 |
42 | private test() {
43 |
> 44 | fs.readFile(this.jsonPath, function (err, buffer) {
| ^
【问题讨论】:
-
您应该使用
<input type="file">元素并让用户选择文件。浏览器中的 Javascript 不能在未经用户许可的情况下选择文件。
标签: reactjs typescript fs