Way to read filenames from directory and output to JSON with Electron & React

生来就可爱ヽ(ⅴ<●) 提交于 2021-01-29 08:47:59

问题


I'm trying to make a music player application with Electron & React. I have been able to access music files in a resources/music folder when built and installed but I have to give the exact path to an mp3.

I have been trying ways to scan the directory and get all music file names to put in a JSON as the idea is the user could add their own music files to the music folder to be picked up by the app and displayed in a list.

Most solutions I see use node fs but i just cnat get it to work. I keep getting TypeError: fs.readdir is not a function

Any help would be greatly appreciated.

import React, { Component } from 'react';
import { Link } from "react-router-dom"
import Testpage from "./Test";
import song from '../Music/Taxi.mp3'
import FileSearch from "../filesearch";

const fileSearch = new FileSearch();

const dataPath =
    process.env.NODE_ENV === 'development'
        ? song
        : 'file:///resources/Music/Taxi.mp3';




class Home extends Component {
    constructor(props) {
        super(props);

        this.state = {};
    }

    componentDidMount() {
        fileSearch.search()
    }


    render() {
        return (
            <div id="page">
                <h1>laceHolder Home</h1>
                <div>
                    <Testpage song={dataPath}/>
                </div>
            </div>
        );
    }
}

export default Home;

const testFolder = './';
const fs = require('fs');

export default class FileSearch {

    search = () => {
        fs.readdir(testFolder, (err, files) => {
            files.forEach(file => {
                console.log(file);
            });
        });
    }
}

回答1:


Solved, after hours of looking (im pretty new to react and electron),

I used the below instead of vanilla fs import and it worked!

const remote = window.require('electron').remote;
const electronFs = remote.require('fs');
const electronDialog = remote.dialog;

Thanks for your comments to look at window.require



来源:https://stackoverflow.com/questions/59762662/way-to-read-filenames-from-directory-and-output-to-json-with-electron-react

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!