Skip to content

Instantly share code, notes, and snippets.

@renevall
Last active January 18, 2016 18:21
Show Gist options
  • Save renevall/5bd7055f8959a4c2f714 to your computer and use it in GitHub Desktop.
Save renevall/5bd7055f8959a4c2f714 to your computer and use it in GitHub Desktop.
import {Http, Headers,HTTP_PROVIDERS} from 'angular2/http';
import {Component} from 'angular2/core';
import {NgForm} from 'angular2/common';
import { fileupload } from "./upload"
import 'rxjs/add/operator/map';
@Component({
selector: "upload-form",
templateUrl: "app/upload-form.component.html",
providers: [HTTP_PROVIDERS]
})
export class UploadFormComponent {
submitted = false;
public file2:any; // Will send this to post
constructor(public http: Http){}
onSubmit(){
this.submitted = true;
console.log("Submitted");
console.log(this.file2); //but it shows as empty
var headers = new Headers();
headers.append('Content-Type', undefined);
this.http.post('http://localhost:8080/upload', this.file2,{
headers: headers
})
.map(res => res.json())
.subscribe(
data => console.log(data),
err => console.log(err),
() => console.log('File upload Complete')
);
}
//Trigger on <input type="file" (change)>
fileListener($event: any) {
console.log("file listener");
this.readThis($event.target, this.setData);
}
//callback fuction to in theory access class properties
setData(data:string){
console.log("setdata done");
console.log(data); //data arrives correctly
this.file2 = data; //this seems to be out of scope when onsubmit() is called.
}
readThis(inputValue: any, callback) {
console.log("llego aca 1 ");
var file:File = inputValue.files[0];
var myReader:FileReader = new FileReader();
myReader.onloadend = function(e){
callback(myReader.result); //calls the callback once the load is done
}
myReader.readAsText(file);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment