我正在为我的 API 使用 Angular6 和 NodeJS。我的应用程序是一个学校软件,我应该用他们的注册号保存学生图像。我在 Angular 中使用ng2-file-upload进行文件上传,在NodeJS中使用 Multer,上传工作正常,但我不明白如何用 Student 的注册号重命名文件。我在 Angular 中的网址由学生注册号组成,我只需要知道如何发送该注册号。编号为 NodeJS 并使用 Reg 重命名文件。数字
我的 .html 文件
<input type="file" name="photo" ng2FileSelect [uploader]="uploader" />
<button type="button" class="btn btn-success btn-s"
(click)="uploader.uploadAll()"
[disabled]="!uploader.getNotUploadedItems().length" >
Upload an Image
</button>
我的 .ts 文件
public uploader: FileUploader = new FileUploader({url: URL, itemAlias: 'file'});
ngOnInit() {
this.uploader.onAfterAddingFile = (file) => { file.withCredentials = false; };
this.uploader.onCompleteItem = (item: any, response: any, status: any, headers: any) => {
console.log('ImageUpload:uploaded:', item, status, response);
alert('File uploaded successfully');
};
}
我的 NodeJS Multer 上传:
var storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, 'E:/school')
},
filename: function (req, file, cb) {
cb(null, Date.now() + '-' + file.originalname);
}
});
var upload = multer({ storage: storage });
router.post('/upload', upload.single('file'), (req, res) => {
if (!req.file) {
console.log("No file received");
return res.send({
success: false
});
} else {
console.log('file received');
return res.send({
success: true
})
}
});
提前致谢。任何帮助将不胜感激。