首页 > 其他分享 >前端实现文件上传——angular版本+ant design

前端实现文件上传——angular版本+ant design

时间:2022-11-07 09:12:44浏览次数:36  
标签:const observer any ant design file path 上传 angular

html代码

        <nz-upload *ngIf="uploadParams.parserTypeId==3 || uploadParams.parserTypeId==4" style="margin-left:10px"
        [nzAction]="_uploadPath"
        [nzHeaders]="{ Authorization: 'multipart/form-data' }"
        [nzData]="uploadParams"
        (nzChange)="handleChange($event)"
        [nzBeforeUpload]="beforeFileUpload"
        [nzRemove]="fileListRemove"
        [nzShowUploadList]="icons"
        [nzFileList]="_fileList"
        >
        <button nz-button>
          <i nz-icon nzType="upload"></i>
          上传文件
        </button>
      </nz-upload>

ts代码
/** 上传文件时传递的参数 */
public uploadParams: any = {};
public _uploadPath: any = parserApiPath.uploadFiles;
public _fileList: NzUploadFile[] = [];

/** 上传文件类型 /
public fileType: string = ".csv, .xlsx, .xls"; //文件类型
/
* 选择excel或者json时将表达式置灰 /
public flagExam: boolean = false;
/
* 上传的icon图标 */
icons: NzShowUploadList = {
showPreviewIcon: true,
showRemoveIcon: true,
showDownloadIcon: false
};

public handleChange(e: any): void {
//判断有没有选择上传类型
if (!this._form.value.parserType) {
console.log("请选择类型");
} else {
e.fileList.map((element: any) => {
if(element.response){
this.path.push(element.response.data);
}
});
//将上传重复的文件去重
this.path = Array.from(new Set(this.path));

  const parserOpt = this._currentParserOption;
  if (!parserOpt) {
    return;
  }
  const parserId = parserOpt.origin.id;

  const fieldParserOptions: FieldParserOption[] = [];
  let parameter = parserOpt.origin.dataParserTemplateParameterVO[0];
  for (let pa in this.path){
    const opt: FieldParserOption = {
      parserId: parserId,
      parserTemplateParameterId: parameter.id,
      value: this.path[pa],
    };
    fieldParserOptions.push(opt);
  }
  this._fieldMappingContext.setDataParserParameters(fieldParserOptions);
}

}

//上传前参数判断
beforeFileUpload = (file: any) =>

new Observable((observer: Observer) => {
const isLt100M = file.size! / 1024 / 1024 <= 100;
if (!isLt100M) {
this.modal.error({
nzTitle: "解析失败",
nzContent: "文件大小不要超过100MB!",
});
observer.complete(); return;
}

const fileType =file.name.split(".")[1]
const fileTypes =this._form.value.parserType
let isType =true
if(fileTypes="excel"){
isType = fileType === 'xlsx' || file.type === 'xls'
}else if(fileTypes
="csv"){
isType = fileType === 'csv';
}else{
alert("请核对类型");
}
if (!isType) {
console.log('只支持xlsx、xls、csv类型!');
this.modal.error({
nzTitle: "解析失败",
nzContent: "请核对上传的文件类型!",
});
observer.complete();
return;
}else{
observer.next(true); observer.complete();
}
});rver.complete(); return;
}

//删除文件操作
fileListRemove = (file: any) =>
new Observable((observer: Observer) => {
let that = this;
this.modal.confirm({
nzTitle: "操作确认",
nzContent: "是否确认删除?",
nzOkText: "确定",
nzCancelText: "取消",
nzOnOk() {
//删除操作
console.log(file,"file.response.data")
// console.log(that.path,"file.response.data")
console.log(that.path,"file.response.data")
that.path.splice(that.path.findIndex((item: any) => item == file.url), 1);
that._fileList.splice(that._fileList.findIndex((item: any) => item.url == file.url), 1);
observer.next(true);
observer.complete();
},
nzOnCancel() {
observer.next(false);
observer.complete();
}
});

});

标签:const,observer,any,ant,design,file,path,上传,angular
From: https://www.cnblogs.com/5200flyer/p/16864859.html

相关文章

  • 3D Engine Design for Virtual Globes - patrick cozzi读书笔记
    前言:不要让这本书的标题欺骗你。标题告诉你的是,如果你有兴趣学习高性能和健壮的游戏地形渲染,这本书适合你。如果你对美国国家航空航天局(NASA)世界风能(WorldWind)或谷歌地球(G......
  • Angular 14 inject 函数使用过程中的一些注意事项
    inject函数只能用于构造器阶段,这意味着其只能在构造器函数作用域(constructorfunctionscope)和字段初始化器(fieldinitializers)中使用。下列代码会遇到运行时错误(runtim......
  • Angular 14 新的 inject 函数介绍
    Angular14提供了一些非常有趣的特性:类型化表单(typedforms)、独立组件(standalonecomponents),以及本文将要介绍的内容,即在所谓的构造器阶段(constructorphase)使用inj......
  • dhtmlxgantt甘特图示例
    官方文档:https://docs.dhtmlx.com/gantt/api__refs__gantt.html示例图片:代码:<!DOCTYPEhtml><head><metahttp-equiv="Content-type"content="text/html......
  • 使用vagrant安装虚拟机踩坑记录
    在安装完visualbox和vagrant并且下载配置好本地虚拟机(box)之后,然后init完初始环境之后在启动虚拟机时报了如下错误,没打开Hyper-V服务。然后打开了该服务,但是在重启之后在启......
  • 使用 Angular 14 的 inject 函数优化对 Ngrx 的使用方式
    我们先看Angular里一个常规的使用NgrxStore的例子:上面这段代码的缺陷是,Component作为UI的展现层,直接依赖于作为第三方库的StoreAPI——一个合乎逻辑的措施是,......
  • 云原生之旅 - 7)部署Terrform基础设施代码的自动化利器 Atlantis
    前言前面有几篇文章讲述了如何使用Terraform创建资源(基础设施即代码Terraform快速入门, 使用Terraform创建Kubernetes)以及 Kubernetes时代的包管理工具Helm ......
  • 表单笔记-Antd(Form)
    antd表单使用笔记import{Form}from'antd'exportdefaultconstApp=()=>{constform=Form.useForm();//提交表单constonFinish=(val)=>{//val......
  • vagrant的安装使用入门(mac环境下)
    安装官网教程:https://developer.hashicorp.com/vagrant/tutorials/getting-started/getting-started-project-setupbrewinstallvagrant#使用brew进行安装brewins......
  • React使用Antd自定义主题报错
    安装包"customize-cra":"^1.0.0","customize-cra-less-loader":"^2.0.0","less":"^4.1.3","less-loader":"^11.1.0",修改config-overrides.jsconst{override......