ng2-file-upload插件在ionic3中的使用方法
本文主要说明在ionic3中使用ng2-file-upload插件,上传多种类型文件(图片、word、excel、ppt、mp4等)的使用方法。
1、html代码:
<button ion-button (click)="selectVideo()">添加</button>
<input id="uploadPic" ng2FileSelect [uploader]="uploader" type="file" (change)="handleVideo($event)"/>
2、在页面module.ts文件中引入FileUploaderModule模块,如course.module.ts文件中:
import { FileUploadModule } from "ng2-file-upload";
@NgModule({
imports: [
FileUploadModule
],
declarations: [
AddCourse,
],
providers: [],
})
export class CourseModule {
}
3、添加文件大小及类型限制配置文件config.ts文件。
export const Conf = {
maxFileSize: 104587600,
allowedMimeType:['image/jpeg','image/png','application/msword',
'application/vnd.ms-excel','application/vnd.ms-powerpoint',
'audio/x-mpeg','video/mp4','audio/mpeg','audio/mp3',
'application/x-shockwave-flash','video/x-ms-asf',
'application/pdf',
'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
'application/vnd.openxmlformats-officedocument.presentationml.presentation'
],
}
3、在具体的ts文件中,使用方法,如:course.ts文件:import {FileUploader} from "ng2-file-upload";
// 引入loading、toast公共方法
import {InterActive} from "../../../core/providers/interActive";
import {TranslateService} from '@ngx-translate/core';
import {FileUploader} from "ng2-file-upload";
import { Conf } from "../../../config";
export class AddCourse {
// 初始化文件上传实例化对象
uploader: FileUploader;
constructor(
public translate: TranslateService,
public interActive: InterActive,
) {
i18n 代码国际化,翻译配置
this.message = {
size: "File size exceeds 100M, please upload again.",
file: "File type is not supported. Please upload again.",
};
this.translate.get(this.message.size).subscribe(value => {
this.message.size = value;
});
this.translate.get(this.message.file).subscribe(value => {
this.message.file = value;
}); // 直接添加文件上传监听事件
this.uploadPack();
}
// 触发文件input标签,选择文件
public selectVideo() {
document.getElementById('uploadPic').click();
}
// 选择文件后 处理文件大小和类型限制
public handleVideo(event: any) {
let url = event.target.value;
url = url.split("\\");
// 上传文件队列数量限制,大于1个时,先移除第一个文件
if (this.uploader.queue.length > 1) {
this.uploader.queue[0].remove();
} else {
// 文件大小和类型限制
if (event.target.files[0]) {
let size = event.target.files[0].size;
let mime = event.target.files[0].type;
if (size > Conf.maxFileSize) {
this.interActive.toast(this.message.size);
} else if (_.indexOf(Conf.allowedMimeType, mime) == -1) {
this.interActive.toast(this.message.file);
}
}
}
} public uploadPack() {
// 文件上传的服务器url地址
let url = "http://mail.ym.163.com";
// 文件上传参数配置(maxFileSize)最大文件限制,(allowMimeType)文件类型限制
let options = {
url: url,
removeAfterUpload: true,
method: "POST",
maxFileSize: Conf.maxFileSize,
allowedMimeType: Conf.allowedMimeType,
};
this.uploader = new FileUploader(options);
// 文件上传之前监听事件
this.uploader.onBeforeUploadItem = (fileItem: any) => {
fileItem.method = "post";
fileItem.alias = fileItem.file.name;
};
// 文件上传进度监听事件
this.uploader.onProgressItem = (fileItem: any, progress: any) => {
};
// 文件上传成功监听事件
this.uploader.onSuccessItem = (item: any, response: string, status: number) => { };
// 文件上传附带的其他额外数据
this.uploader.onBuildItemForm = (fileItem: any, form: any) => {
let data = [{
files: [fileItem.file.name],
name:this.name,
....... }];
console.log(fileItem)
form.append('data', JSON.stringify(data));
};
}
// 上传文件
public uploadVideo() {
this.uploader.uploadAll();
}}
}
5、总结:该插件最终是以formData数据格式传给后台的。
ng2-file-upload插件在ionic3中的使用方法的更多相关文章
- jQuery File Upload 插件 php代码分析
jquery file upload php代码分析首先进入构造方法 __construct() 再进入 initialize()因为我是post方式传的数据 在进入initialize()中的po ...
- JQuery File Upload 插件 出现 “empty file upload result” 错误的解决方案。
本例中采用的是 JQuery File Upload + ASP.NET 的方式, Google了大半天基本没有找到合理的解决方案,倒是在 NodeJS的一遍博客中找到了灵感:http://www.i ...
- angularjs file upload插件使用总结
之前由于项目需要,决定使用angularjs做前端开发,在前两个项目中都有文件上传的功能,因为是刚接触angularjs,所以对一些模块和模块间的依赖不是很了解.都是由其他大神搭好框架,我只做些简单的 ...
- jquery file upload + asp.net 异步多文件上传
百度了很久,国内一直 找不到 使用jquery file upload 插件 +asp.net 的相关代码 一开始使用 jquery uploadify ,一款基于 flash的插件,但是不支持 Sa ...
- jQuery中的$.extend方法总结
原文见:jQuery.extend()函数详解 Jquery的扩展方法extend是我们在写插件的过程中常用的方法,但是经常容易搞不清楚以下两个写法的关系: 1.$.extend(dest,src1, ...
- jQuery File Upload 文件上传插件使用一 (最小安装 基本版)
jQuery File Upload 是一款非常强大的文件上传处理插件,支持多文件上传,拖拽上传,进度条,文件验证及图片音视频预览,跨域上传等等. 可以说你能想到的功能它都有.你没想到的功能它也有.. ...
- jQuery File Upload文件上传插件简单使用
前言 开发过程中有时候需要用户在前段上传图片信息,我们通常可以使用form标签设置enctype=”multipart/form-data” 属性上传图片,当我们点击submit按钮的时候,图片信息就 ...
- jquery file upload 文件上传插件
1. jquery file upload 下载 jquery file upload Demo 地址:https://blueimp.github.io/jQuery-File-Upload/ jq ...
- jQuery File Upload 单页面多实例的实现
jQuery File Upload 的 GitHub 地址:https://github.com/blueimp/jQuery-File-Upload 插件描述:jQuery File Upload ...
随机推荐
- ArrayList、LinkedList、Vector区别
ArrayList.LinkedList.Vector均为可伸缩数组,即可以动态改变长度的数组. 比较ArrayList和Vector: 1. 共同点: ArrayList和Vector都是基于Obj ...
- 【Leetcode周赛】从contest-91开始。(一般是10个contest写一篇文章)
Contest 91 (2018年10月24日,周三) 链接:https://leetcode.com/contest/weekly-contest-91/ 模拟比赛情况记录:第一题柠檬摊的那题6分钟 ...
- Java两个引用指向同一个数组
- elasticsearch相关聚合查询示例
索引(index):logstash-nginx-*,type:nginx_access 请求路径: 1.按照某个字段进行分组统计访问量 { "query": { "bo ...
- Unparseable date: "Mon Aug 15 11:24:39 CST 2016",时间格式转换异常
String datestr= "Mon Aug 15 11:24:39 CST 2016";//Date的默认格式显示 Date date=new SimpleDateForma ...
- GET和POST是HTTP请求的两种基本方法,区别是什么!?
GET和POST是HTTP请求的两种基本方法,要说它们的区别,接触过WEB开发的人都能说出一二. 最直观的区别就是GET把参数包含在URL中,POST通过request body传递参数. 你可能自己 ...
- CSS中的flex布局
1.flex 布局的概念 Flex是Flexible Box的缩写,意为"弹性布局",用来为盒状模型提供最大的灵活性.任何一个容器都可以指定为 Flex 布局,行内元素也可以通过 ...
- python3安装pdfminer并使用
1.python3不同与2版本不能使用pdfminer pip install pdfminer3k 2.使用pdfminer解析相应文档并保存到相应的文件夹中 # encoding : udf-8 ...
- Review: 9-13 July
9 July 并查集 int fa[]; for (int i=1; i<=n; ++i) fa[i]=i; int f(int x){return fa[x]==x?x:fa[x]=f(fa[ ...
- speike
speike 题目描述 众所周知,Speike 狗是一条特别喜欢追着Tom 打的狗. 现在,Tom 又把Speike 惹生气了,现在Speike 需要跨越千山万水找Tom 报仇. Speike 所在的 ...