2017-04-26 3 views
1

나는 angular2뿐만 아니라 오버플로도 새로 추가했습니다. 현재 ang2를 사용하는 응용 프로그램 내부의 로컬 폴더에 이미지를 업로드하는 방법을 배우고 있습니다. 로컬 폴더에 이미지를 저장하는 방법을 이해할 수 없습니다. 스택 오버플로에서 많은 답변을 읽었지만 모두 (거의) angular2가 아닌 angularj를 사용합니다. 어떤 사람이 angular2 및 nodejs를 사용하여 로컬 폴더에 이미지를 업로드하고 저장하는 방법을 알려주십시오. html 코드가 있습니다.angular2를 사용하여 로컬 폴더에 이미지를 저장하는 방법

<div class="form-group"> 
<label for="single">single</label> 
<input type="file" class="form-control" name="single" 
/></div> 

나는 완전히 헛되이 낭비했다. 누군가가 내게 2자를 사용하여 로컬 폴더에 저장하는 방법을 보여줌으로써 간단한 튜토리얼을 줄 수 있습니까

+1

본 적이 있습니까? http://stackoverflow.com/questions/41547945/write-to-a-local-file-using-angular2-typescript-locally – DeborahK

+0

나는 그것을 끝 마쳤습니다. 백엔드 용 django 사용. – maadi

답변

0

업로드 할 파일을 선택하면 이미 로컬에서 선택되고 있기 때문에 서버 폴더에 이미지를 저장하는 방법을 의미한다고 생각합니다. 폴더. 권리? 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를 고려 중이 야)를 실행 것입니다.

관련 문제