本文主要说明在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. 毕设问题(2) fastjson 的过滤器使用(SSH hibernate)以及转换对象出现 $ref

    毕业设计,用SSH框架,但是想要做出异步请求数据的效果,使用了ajax方式,其中数据传递的类型为json.ajax使用用的jQuery的ajax.其实struts里也支持ajax功能,但是我觉得用太多 ...

  2. Android作业list

    作业1. 请在自己的电脑上完成Android的安装与配置,并完成Hello Android项目.上交自己与项目的合照,将照片传至QQ群中. ------------------------------ ...

  3. "=="、equals、hashCode之间的区别

    1. "=="分为两种情况: (1) 基本数据类型,比较的是其对应的值是否相等: (2) 引用类型,比较的是他们在内存中存放的地址(或者说,是否指向同意对象). 2. equals ...

  4. procixx地址

    \\192.168.35.7\Download\Builds\procixx_psoc

  5. javascript 回到顶部效果的实现

    demo.js window.onload=function() { var timer=null; var obtn=document.getElementById('btn'); var isTo ...

  6. Flutter的flutter_calendar日曆的使用

    效果: 添加依賴: flutter_calendar: ^0.0.1 項目中導入 import 'package:flutter_calendar/flutter_calendar.dart'; 例子 ...

  7. Java中的String、StringBuffer、StringBuilder区别以及Java之StringUtils的用法

    1.String.StringBuffer.StringBuilder的区别 String是Java中基础类型,是immutable类(不可变)的典型实现,利用string进行拼接是会产生过多无用对象 ...

  8. Xcode7.1环境下上架iOS App到AppStore 流程①

    前言部分 之前App要上架遇到些问题到网上搜上架教程发现都是一些老的版本的教程 ,目前iTunesConnect 都已经迭代好几个版本了和之前的 界面风格还是有很大的差别的,后面自己折腾了好久才终于把 ...

  9. linux根据进程名获取PID

    经常需要Kill多个进程,这些进程包含共同的关键字,可以用一条命令Kill掉它们. ps aux | grep "common" |grep -v grep| cut -c 9-1 ...

  10. vector内存增长方式

    首先必须要了解vector是一种特殊的数组,因此其内存必然是连续的 其次它的连续是建立在不断地对内存的预分配上的,即不断地销毁当前,重新建立内存,效率有点低.所以存在几个函数capacity, siz ...