fileChange(event) { let fileList: FileList = event.target.files; if(fileList.length > 0) { let file: File = fileList[0]; let formData:FormData = new FormData(); formData.append('uploadFile', file, file.name); let headers = new Headers(); /** No need to include Content-Type in Angular 4 */ headers.append('Content-Type', 'multipart/form-data'); headers.append('Accept', 'application/json'); let options = new RequestOptions({ headers: headers }); this.http.post(`${this.apiEndPoint}`, formData, options) .map(res => res.json()) .catch(error => Observable.throw(error)) .subscribe( data => console.log('success'), error => console.log(error) ) } }
node server에서 수신하기
1 2 3 4 5 6 7 8 9 10 11 12 13
router.post('/cover_img',function(req,res){ console.log("커버이미지 저장 시작!"); var form = new formidable.IncomingForm(); form.parse(req, function (err, fields, files) { console.log(files); var oldpath = files.uploadFile.path; var newpath = "/var/www/html/contents_cover_img/" + files.uploadFile.name; fs.rename(oldpath, newpath, function (err) { if (err) throw err; res.sendStatus(200); }); }) })
Comments