加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angularjs 上传

发布时间:2020-12-17 08:18:19 所属栏目:安全 来源:网络整理
导读:xxx.module.ts模块 import { NgModule} from “@angular/core”; import { FileUploadModule } from “ng2-file-upload” ; import { XXXComponent } from “./xxx.component”; @NgModule({ imports:[ FileUploadModule ], declarations:[ XXXComponent ,/

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读