简体   繁体   English

如何将图像文件移动到文件夹?

[英]How can I move image files to a folder?

I'm currently working on a ReactJS web app. 我目前正在使用ReactJS网络应用程序。 I'm trying to upload a images to a folder and store the filename in the database so I can work with it. 我正在尝试将图像上传到文件夹并将文件名存储在数据库中,以便可以使用它。

Everything works so far, but my only blockade is the file transfer to the folder. 到目前为止,一切正常,但是我唯一的障碍是将文件传输到文件夹。

Here the ImageUpload component: 这里是ImageUpload组件:

    import * as React from 'react';

   export class ImageUpload extends React.Component<any, any> {
    constructor(props : any) {
        super(props);
        this.state = { file: '', imagePreviewUrl: '' };
    }

    _handleSubmit(e : any) {
        e.preventDefault();

        // TODO: do something with -> this.state.file
        alert(this.state.file.name);
        this.state.file.copy("C:\\Documents");
    }

    _handleImageChange(e : any) {
        e.preventDefault();

        let reader = new FileReader();
        let file = e.target.files[0];

        reader.onloadend = () => {
            this.setState({
                file: file,
                imagePreviewUrl: reader.result
            });
        }

        reader.readAsDataURL(file)
    }

    render() {
        let { imagePreviewUrl } = this.state;
        let $imagePreview = null;
        if (imagePreviewUrl) {
            $imagePreview = (<img src={imagePreviewUrl} />);
        } else {
            $imagePreview = (<div className="previewText">Please select an Image for Preview</div>);
        }

        return (
            <div className="previewComponent">
                <form onSubmit={(e) => this._handleSubmit(e)}>
                    <input className="fileInput"
                        type="file"
                        onChange={(e) => this._handleImageChange(e)} />
                    <button className="submitButton"
                        type="submit"
                        onClick={(e) => this._handleSubmit(e)}>Upload Image</button>
                </form>
                <div className="imgPreview">
                    {$imagePreview}
                </div>
            </div>
        )
    }
}

As you can see I'm trying to copy or move the file to C:\\Documents . 如您所见,我正在尝试将文件复制或移动到C:\\ Documents I didn't manage to find the solution to properly move the file to the folder. 我没有设法找到将文件正确移动到文件夹的解决方案。

Any suggestions? 有什么建议么?

If you want to upload an image and save it to database, you need an API which will accept FormData, see Form Data Usage , then on the server you will need to save that file to destination. 如果要上传图像并将其保存到数据库,则需要一个可以接受FormData的API,请参阅Form Data Usage ,然后在服务器上需要将该文件保存到目标位置。 For that you could use ExpressJS and middleware to handle multipart upload - from there you can use NodeJS functions to save file to local destination. 为此,您可以使用ExpressJS和中间件来处理分段上传 -从那里可以使用NodeJS函数将文件保存到本地目标位置。

It's not possible to do using only react. 仅使用反应是不可能的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我可以将 package.json 移动到文件夹中吗? - Can I move package.json into a folder? 如何移动图像下方的绿色文本? - How can I move the green text under the image? 如何使用webpack将文件复制到分发文件夹? - How can I use webpack to copy files to the distribution folder? ESLint检查文件夹内的文件,但不检查文件夹内的文件夹。 我该如何解决? - ESLint checks files inside a folder but doesn't check the folders inside the folder. How can I fix it? 如何从 react.js 中我的 react-app 文件夹的外部文件夹中调用图像 - How can I call image from outer folder of my react-app folder in react.js 我可以将 node_modules 文件夹移动到另一个项目吗? - Can i move my node_modules folder to another project? 我如何为图像文件设置 React initialState - how can i set React initialState for image files 如何在 React Native 样式中将我的内容图像上移? - How can I move my content image upper in React Native styling? 如何在 React + React-Router 应用程序中从文件夹中获取 JSON 文件? - How can I fetch JSON files from a folder, in a React + React-Router app? 如何将特定文件夹中的所有(mp3)文件导入 react.js 中的数组? - How can I import all (mp3) files from a particular folder into an array in react.js?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM