如何将模板中的数据附加到Angular2

时间:2016-08-19 07:25:46

标签: angular typescript image-uploading

我正在尝试上传图片,但我不确定我的方法,如何将模板中的图像附加到控制台?有人可以建议如何取悦吗?

我的模板:

<input type="file" ng2FileSelect [uploader]="uploader" />

我的组件:

   import {Component} from '@angular/core';
   import {Http, Response, Headers} from '@angular/http';
   import {FILE_UPLOAD_DIRECTIVES, FileUploader} from '../ng2-file-upload/ng2-file-upload';
 const URL = 'http://localhost/a2server/index.php/profile/upload';
 @Component({
    selector:'Professional',
    templateUrl:'./components/professional/professional.html',
    directives: [FILE_UPLOAD_DIRECTIVES]
 })

  export class Professional{


     constructor(public http: Http){
     var uploader:FileUploader;
       var headers = new Headers();
       headers.append('Content-Type', 'application/x-www-form-urlencoded')
    // var str = localStorage.getItem('social');
    // var loc = JSON.parse(str);
    // var id = loc.profile_id;
         this.http.post('http://localhost/a2server/index.php/profile/upload', uploader, { headers: headers })
        .subscribe(
        response => {
            if (response.json().error_code == 0) {
              alert('success');
            }
            else {
              alert('fail');

            }
        })  
    }
}

0 个答案:

没有答案