【发布时间】:2021-03-30 02:39:57
【问题描述】:
我正在使用nestjs 和react js 来制作图片上传器。对于反应,我使用这个:
const props = {
onChange({ file, fileList }: any) {
const fd = new FormData();
fd.append('img', fileList[0].originFileObj);
fetch('http://localhost:3000/upload', {
method: 'POST',
body: fd,
})
.then(async response => {
const data = await response.json();
console.log(data)
})
.catch(error => {
message.error('There was an error!', error);
});
},
defaultFileList: [
],
};
return (
<div>
<Form
name="form"
className="form"
>
<Form.Item
className="upload"
valuePropName="fileList"
>
<Upload {...props}>
<Button>Upload</Button>
</Upload>
</Form.Item>
</Form>
</div>
);
};
对于我使用的 NestJs:
@Controller()
export class AppController {
constructor(private readonly appService: AppService) {}
@Post('/upload')
@UseInterceptors(
FileInterceptor('img', {
dest: './uploads',
}),
)
uploadFile(@UploadedFile() file) {
console.log(file, 'file');
}
}
在console.log(file, 'file'); 我得到:
{
fieldname: 'img',
originalname: 'Full.jpg',
encoding: '7bit',
mimetype: 'image/jpeg',
destination: './uploads',
filename: '3c45cd07145707ec09235c7bf3954d7f',
path: 'uploads\\3c45cd07145707ec09235c7bf3954d7f',
size: 60655
}
问题:我应该将上面对象中的哪些信息发送回前端,哪些信息要存储在数据库中?
注意:现在,在 NestJs 应用程序的 uploads 文件夹中,我在上传图像后存储了 3c45cd07145707ec09235c7bf3954d7f。我怎么知道,我应该在我的服务器上发送数据,服务器应该返回有效的文件(图像)。
谁能帮忙?
【问题讨论】:
标签: javascript reactjs nestjs