[英]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.