本文主要说明在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. Beta-星期五

    所属课程 https://edu.cnblogs.com/campus/xnsy/SoftwareEngineeringClass 作业要求  https://edu.cnblogs.com/camp ...

  2. Sass函数-comparable 判断两个数是否可进行加减、合并

    comparable() 函数主要是用来判断两个数是否可以进行“加,减”以及“合并”.如果可以返回的值为 true,如果不可以返回的值是 false: >> comparable(2px, ...

  3. canvas 操作像素 反相

    代码实例: <!DOCTYPE html> <html> <head> <style> canvas{ background:#eee; } </ ...

  4. 2、pycharm中设置pytest为默认运行

    1.打开File-setting 2.打开Tools-Python Integrated Tools 3.找到Default test runner选项,在下拉框中选择py.test 4.点Apply ...

  5. python处理文件某行的固定位置

    1.打开文件 2.按行循环 3.处理固定行 with open('file/Aa.txt') as f: for line in f: print(line[2:12]) 可以这样处理的原因是,lin ...

  6. BestCoder Round #92 (hdu 6015 6016)

    比赛链接 A题主要是map的使用,比赛的时候问了下队友,下次要记住了 #include<bits/stdc++.h> using namespace std; typedef long l ...

  7. sql判断中文、数字、英文

    IF OBJECT_ID('DBO.GET_NUMBER2') IS NOT NULL DROP FUNCTION DBO.GET_NUMBER2 GO )) ) AS BEGIN BEGIN ,'' ...

  8. Code Review怎样做好

    一.背景 最近随着交易业务快速扩展,研发组内新项目及新成员越来越多,如何做好Code Review,把控研发人员开发代码质量很是关键. 对于大部分业务团队,谈到Code Review就会面露哀状:   ...

  9. vue项目打包之后原本好的样式变得不好了的原因分析

    这个主要是打包的过程将所有的css文件进行归类压缩,导致原先其他文件里的样式对当前的产生了影响,应该有同样的类名了.怎么改?要么改类名,要么用scope,scss的写法.

  10. CF 452E. Three strings(后缀数组+并查集)

    传送门 解题思路 感觉这种题都是套路之类的??首先把三个串并成一个,中间插入一些奇怪的字符,然后跑遍\(SA\).考虑按照\(height\)分组计算,就是每个\(height\)只在最高位计算一次, ...