参考资料:

http://www.imooc.com/article/40038

https://github.com/xkeshi/image-compressor

示例代码:

<nz-upload class="avatar-uploader" [nzAccept]="'image/*'" nzFileType="image/png,image/jpeg,image/gif,image/jpg" [nzAction]="uploadPictureUrl" nzName="avatar" nzListType="picture-card" [nzShowUploadList]="false" [nzCustomRequest]="uploadImp" (nzChange)="handleChange($event)">
<ng-container *ngIf="!imageUrl">
<i nz-icon nzType="picture" nzTheme="outline"></i>
<div class="ant-upload-text">上传</div>
</ng-container>
<img *ngIf="imageUrl" [src]="imageUrl" class="avatar" style="width:100px;height: 100px;">
</nz-upload>
<p>请上传jpg, gif, png格式的图片。建议图片尺寸&nbsp;宽:90px;高:90px</p>
import ImageCompressor from 'image-compressor.js'
// 自定义上传方法的实现
uploadImp = async (item) => {
debugger
const isJPG = item.file.type.indexOf('image') > -;
if (!isJPG) {
this.message.error('只能上传图片文件!');
return;
}
// 进行图片压缩
const compressionFile = await new ImageCompressor().compress(item.file, {
quality: .,
maxWidth: ,
maxHeight: ,
convertSize: , //超过600kb压缩
success(result) {
},
error(e) {
console.log(e);
debugger
throw { message: `压缩失败${e.message}` }
}
}).then(ret => {
debugger
console.log(ret);
item.file = ret;
if (ret.size > * ) throw { message: '压缩后的图片不能超过600KB' };
const formData = new FormData();
formData.append('file', item.file, item.file.name);
this.http.post(this.uploadPictureUrl, formData).subscribe(result => {
debugger
});
}).catch((err) => {
const msg = err.message ? err.message : err;
this.message.error(`图片上传失败,请重试:${msg}`);
});
}
public string UploadImgToOss()
{
var file = Request.Form.Files.FirstOrDefault();
if (file == null)
throw new UserFriendlyException(L("FileInfo_Change_Error"));
var fileInfo = new FileInfo(file.FileName); var fileExt = Path.GetExtension(file.FileName);
byte[] fileBytes; string url = "";
using (var stream = file.OpenReadStream())
{ fileBytes = stream.GetAllBytes(); if (!ImageFormatHelper.GetRawImageFormat(fileBytes).IsIn(ImageFormat.Jpeg, ImageFormat.Png, ImageFormat.Gif))
{
throw new UserFriendlyException("请上传图片文件,仅接受Jpg、PNG、Gif三种格式!");
} var upLoadPath = "/Upload/" + DateTime.Now.ToString("yyyy") + "/" + DateTime.Now.ToString("MM") + "/";
var newFileName = $"{DateTime.Now:yyyyMMddHHmmss}_{Guid.NewGuid():n}_{fileExt}"; var data = new MemoryStream(fileBytes);
var result = OssDrive.UpLoad(newFileName, data);
if (!result.Status)
{
throw new UserFriendlyException(result.Message);
}
url = "http://" + result.Data.Url;
}
return url;
}

Angular里使用(image-compressor.js)图片压缩的更多相关文章

  1. 使用ajax上传图片,支持图片即时浏览,支持js图片压缩后上传给服务器

    使用ajax上传图片,支持图片即时浏览,支持js图片压缩后上传给服务器 ajax上传主要使用了 var reader = new FileReader() 此方法 js图片压缩主要是利用canvas进 ...

  2. js图片压缩和上传并显示

    由于近期项目中需要做个图片压缩上传,所以就在网上找了些资料自己写了一个 html部分 <input id="file" type="file"> & ...

  3. js 图片压缩上传(base64位)以及上传类型分类

    一.input file上传类型 1.指明只需要图片 <input type="file" accept='image/*'> 2.指明需要多张图片 <input ...

  4. js图片压缩+ajax上传

    图片压缩用到了localresizeimg 地址: https://github.com/think2011/localResizeIMG 用起来比较简单 <input type="f ...

  5. JS—图片压缩上传(单张)

    *vue+webpack环境,这里的that指到vue实例 <input type="file" name="file" accept="ima ...

  6. 无组件客户端js图片压缩

    <div class="free-upload"> <p>上传您的约会照片,一张合影.一张票据哦!</p> <div class=&quo ...

  7. js 图片压缩上传(纯js的质量压缩,非长宽压缩)

    下面是大神整理的demo,很实用,这里存一下备用,感谢大神! 此demo为大于1M对图片进行压缩上传 若小于1M则原图上传,可以根据自己实际需求更改. demo源码如下 <!DOCTYPE ht ...

  8. js图片压缩

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  9. js图片压缩上传

    最近公司的移动产品相约app要做一次活动,涉及到图片上传,图片又不能太大,不然用户体验太差,必须先压缩再上传,所以用到了html5的canvas和FileReader,代码先上,小弟前端经验不足,代码 ...

随机推荐

  1. 题解 [51nod1461] 稳定桌

    题面 解析 一开始看上去毫无思路啊. 但想通了就很很简单. 我们枚举每种长度的边作为最大的边, 于是长度比当前大的边就要砍掉, 而长度比当前小的边只能留下小于等于当前长度的边数, 砍掉代价最小的几条就 ...

  2. 爬虫代理池源代码测试-Python3WebSpider

    元类属性的使用 来源: https://github.com/Python3WebSpider/ProxyPool/blob/master/proxypool/crawler.py 主要关于元类的使用 ...

  3. 和PHP相关的Linux命令

    Linux服务器上怎么找到php.ini php -ini #输出一堆信息,里面有loaded configuration file => /etc/php/7.0/cli/php.ini就是了 ...

  4. SpringBoot+Mybatis-Plus两种分页方法

    用到的依赖: <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus ...

  5. dijkstra之zkw线段树优化

    其实特别好理解,我们只要写一个数据结构(线段树)支持一下操作: 1.插入一个数\(x\). 2.查询当前数据结构中最小的数的插入编号. 3.删除插入编号为\(x\)的数. 第一眼看成可持久化了 其实就 ...

  6. 【luoguP4124 】[CQOI2016]手机号码

    题目描述 人们选择手机号码时都希望号码好记.吉利.比如号码中含有几位相邻的相同数字.不含谐音不吉利的数字等.手机运营商在发行新号码时也会考虑这些因素,从号段中选取含有某些特征的号码单独出售.为了便于前 ...

  7. Maximum GCD(UVA 11827)

    Problem:Given the N integers, you have to find the maximum GCD (greatest common divisor) of every po ...

  8. Python数据抓取(1) —数据处理前的准备

    (一)数据抓取概要 为什么要学会抓取网络数据? 对公司或对自己有价值的数据,80%都不在本地的数据库,它们都散落在广大的网络数据,这些数据通常都伴随着网页的形式呈现,这样的数据我们称为非结构化数据 如 ...

  9. 【java设计模式】-05建造者模式

    建造者模式 简述 建造者模式,是将一个复杂对象的创建和它的表示分离开来,这就使得同样的构建构成可以有不同的表示. 建造者模式是一步步构建一个复杂的对象,允许用户只需要指定复杂对象的类型和必要的内容就可 ...

  10. PHP Storm Built In Server Doesn't Recognize mod_rewrite

    http://stackoverflow.com/questions/22139032/php-storm-built-in-server-doesnt-recognize-mod-rewrite 版 ...