【发布时间】:2020-12-05 03:43:36
【问题描述】:
我花了最后 3 天的时间来解决问题,但我还没有弄清楚问题。
Angular CLI:6.0.8
节点:8.11.2
操作系统:win32 x64
角度:6.0.6
穆尔特。 1.3.1
我在“childApi”上使用 multer 人员的代码:
var store = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, './uploads');
},
filename: function (req, file, cb) {
cb(null, Date.now() + '.' + file.originalname);
}
});
var upload = multer({ storage: store , }).single('file');
router.post('/upload', function (req, res, next) {
upload(req, res, function (err) {
if (err) {
return console.log ('not working well')
}
//do all database record saving activity
return res.json({ originalname: req.file.originalname, uploadname: req.file.filename });
});
});
我在“add-child”组件中使用简单代码的代码:
import { Component, OnInit, Inject } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { MatDialog, MatDialogRef, MAT_DIALOG_DATA } from '@angular/material';
import { Child } from '../../models/child';
import { ChildService } from '../../services/child.service';
import {FileUploader } from 'ng2-file-upload';
const uri = 'http://localhost:3000/childApi/upload';
@Component({
selector: 'app-add-child',
templateUrl: './add-child.component.html',
styleUrls: ['./add-child.component.css']
})
export class AddChildComponent implements OnInit {
newChild = new Child;
uploader: FileUploader = new FileUploader({ url: uri });
attachmentList: any = [];
constructor(private childService: ChildService,
private route: ActivatedRoute,
private router: Router,
public dialogRef: MatDialogRef<AddChildComponent>,
@Inject(MAT_DIALOG_DATA) public data: any) {
this.uploader.onCompleteItem = (item: any, response: any, status: any, headers: any) => {
this.attachmentList.push(JSON.parse(response));
};
}
问题是我将文件上传到“上传”文件夹后 ,我想在屏幕上显示我的新照片。
控制台给我这个错误:
GET unsafe:C:\fakepath\child+thinking.jpg 0 ()
如果有人帮忙,那就太棒了。
谢谢...
【问题讨论】:
-
问题解决了:
标签: javascript typescript cors angular6 multer