업로드 할 파일을 선택하면 이미 로컬에서 선택되고 있기 때문에 서버 폴더에 이미지를 저장하는 방법을 의미한다고 생각합니다. 폴더. 권리? NodeJS를 언급하여 NodeJS 서버로 보내고 저장하려고한다. 나는이 시작하는 것을 당신을 도움이되기를 바랍니다
updateImage(fileToUpload) {
const body = JSON.stringify(fileToUpload);
const headers = new Headers({ 'Content-Type': 'image/jpeg' });
return this.http.patch(this.config.host + '/photo/', body, { headers: headers })
.map((response: Response) => {
console.log(response.json().data);
})
.catch((error: Response) => {
console.log("Error: ", error);
return Observable.throw(error.json());
});
}
:
import { Component, OnInit, OnDestroy, ChangeDetectorRef } from '@angular/core';
...
fileToUpload: File;
constructor(private imageService: ImageService,
private changeDetectorRef: ChangeDetectorRef) { }
fileChange(input) {
this.readFiles(input.files);
}
readFile(file, reader, callback){
reader.onload =() => {
callback(reader.result);
this.fileToUpload = reader.result;
this.onImageUpload();
}
reader.readAsDataURL(file);
}
readFiles(files, index=0){
let reader = new FileReader();
if (index in files) {
this.readFile(files[index], reader, (result) => {
var img = document.createElement("img");
img.src = result;
this.readFiles(files, index+1);
});
} else {
this.changeDetectorRef.detectChanges();
}
}
onImageUpload() {
this.imageService.updateImage(this.fileToUpload).subscribe(
(photo: Photo) => {
if (photo) { ... }
}
);
}
그런 다음 서비스에서이 같은 것을 (나는 JPG를 고려 중이 야)를 실행 것입니다.
본 적이 있습니까? http://stackoverflow.com/questions/41547945/write-to-a-local-file-using-angular2-typescript-locally – DeborahK
나는 그것을 끝 마쳤습니다. 백엔드 용 django 사용. – maadi