本文主要说明在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中的使用方法的更多相关文章

  1. jQuery File Upload 插件 php代码分析

    jquery file upload php代码分析首先进入构造方法 __construct() 再进入 initialize()因为我是post方式传的数据  在进入initialize()中的po ...

  2. JQuery File Upload 插件 出现 “empty file upload result” 错误的解决方案。

    本例中采用的是 JQuery File Upload + ASP.NET 的方式, Google了大半天基本没有找到合理的解决方案,倒是在 NodeJS的一遍博客中找到了灵感:http://www.i ...

  3. angularjs file upload插件使用总结

    之前由于项目需要,决定使用angularjs做前端开发,在前两个项目中都有文件上传的功能,因为是刚接触angularjs,所以对一些模块和模块间的依赖不是很了解.都是由其他大神搭好框架,我只做些简单的 ...

  4. jquery file upload + asp.net 异步多文件上传

    百度了很久,国内一直 找不到 使用jquery file upload 插件 +asp.net 的相关代码 一开始使用 jquery uploadify ,一款基于 flash的插件,但是不支持 Sa ...

  5. jQuery中的$.extend方法总结

    原文见:jQuery.extend()函数详解 Jquery的扩展方法extend是我们在写插件的过程中常用的方法,但是经常容易搞不清楚以下两个写法的关系: 1.$.extend(dest,src1, ...

  6. jQuery File Upload 文件上传插件使用一 (最小安装 基本版)

    jQuery File Upload 是一款非常强大的文件上传处理插件,支持多文件上传,拖拽上传,进度条,文件验证及图片音视频预览,跨域上传等等. 可以说你能想到的功能它都有.你没想到的功能它也有.. ...

  7. jQuery File Upload文件上传插件简单使用

    前言 开发过程中有时候需要用户在前段上传图片信息,我们通常可以使用form标签设置enctype=”multipart/form-data” 属性上传图片,当我们点击submit按钮的时候,图片信息就 ...

  8. jquery file upload 文件上传插件

    1. jquery file upload 下载 jquery file upload Demo 地址:https://blueimp.github.io/jQuery-File-Upload/ jq ...

  9. jQuery File Upload 单页面多实例的实现

    jQuery File Upload 的 GitHub 地址:https://github.com/blueimp/jQuery-File-Upload 插件描述:jQuery File Upload ...

随机推荐

  1. Linux就该这么学06学习笔记

    参考链接:https://www.linuxprobe.com/chapter-06.html 1.一切从“/”开始 Linux系统中的一切文件都是从“根(/)”目录开始的,并按照文件系统层次化标准( ...

  2. neuoj Blurred Pictures(小思维题

    https://oj.neu.edu.cn/problem/1505 题意:一张由n*n的照片,每行从第ai个像素点到第bi个像素点是非模糊点,要求找出最大的正方形,该正方形中的像素都是非模糊点. 思 ...

  3. AbstractQueuedSynchronizer简单使用

    AQS是JUC中很多同步组件的构建基础,简单来讲,它内部实现主要是状态变量state和一个FIFO队列来完成,同步队列的头结点是当前获取到同步状态的结点,获取同步状态state失败的线程,会被构造成一 ...

  4. .net core 集成极光推送

    登录极光推送 创建应用 appkey和master secret在推送时会使用 设置推送 使用手机扫描二维码安装apk 下载dll 测试 using Jiguang.JPush; using Jigu ...

  5. PL/SQL-FOR UPDATE 与 FOR UPDATE OF的区别

    PL/SQL-FOR UPDATE 与 FOR UPDATE OF的区别 数据库 oracle for update of   和   for update区别     select * from T ...

  6. 正确读取resources目录下的文件

    问题描述:本地可以正常读取areacode.json文件,打成jar包在测试环境找不到该文件. 问题代码: static { StringBuffer strbuffer = new StringBu ...

  7. sql语句中判断空值的函数

    COALESCE()函数 主流数据库系统都支持COALESCE()函数,这个函数主要用来进行空值处理,其参数格式如下:  COALESCE ( expression,value1,value2……,v ...

  8. max_length 属性

    错误:漏掉了 max_length  属性 ERRORS:users.UserProfile.image: (fields.E210) Cannot use ImageField because Pi ...

  9. 51nod 1831: 小C的游戏(Bash博弈 找规律)

    题目链接 此类博弈不需要考虑sg函数,只需要确定必胜态和必败态,解题思路一般为打败先打表找规律,而后找规律给出统一的公式.打表方式:给定初始条件(此题中为ok[0]=ok[1]=0),然后从低到高枚举 ...

  10. Linux 多个cpp文件的编译(Makefile)

    打包so文件: CC = g++ CFLAGS=-Wall -O2 -fPIC TARGET = libbg.so SRCS := $(wildcard *.cpp) OBJS := $(patsub ...