angularjs 上传

前端之家收集整理的这篇文章主要介绍了angularjs 上传前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

xxx.module.ts模块
import { NgModule} from “@angular/core”;
import { FileUploadModule } from “ng2-file-upload” ;
import { XXXComponent } from “./xxx.component”;

@NgModule({
imports:[
FileUploadModule
],
declarations:[
XXXComponent ,/component,pipe,directive/],
providers:[]
})
export class XXXModule{}

xxx.component.ts组件
import { Component} from “@angular/core”;
import { FileUploader} from “ng2-file-upload/file-upload/file-uploader.class”;

@Component({
templateUrl:’xxx.component.html’
})

export class XXXComponent{
constructor(){ }
public fileList;
public uploadFile(event){
this.fileList = event.target.files;
}

//上传
public submitUploadFile(){
if(this.fileList.length>0){
let file:File = this.fileList[0];
let formData = new FormData();
formData.append(‘file’,file,file.name);
let headers = new Headers();
headers.append(‘Accept’,’application/json’);
let options = new RequestOptions({headers:headers});
this.http.post(url,formData,options)
.map(res=>res.json())
.catch(error=>Observable.throw(error))
.subscribe(

//返回上传数据
  • 1
//success or fail
  • 1
)
  • 1
  • 2

}
}
}

xxx.component.html

原文链接:https://www.f2er.com/angularjs/144786.html

猜你在找的Angularjs相关文章