惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - 【当耐特】
Help Net Security
Help Net Security
P
Proofpoint News Feed
J
Java Code Geeks
爱范儿
爱范儿
Last Week in AI
Last Week in AI
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
F
Full Disclosure
Google DeepMind News
Google DeepMind News
H
Help Net Security
G
Google Developers Blog
Jina AI
Jina AI
Vercel News
Vercel News
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
L
Lohrmann on Cybersecurity
S
Schneier on Security
Microsoft Azure Blog
Microsoft Azure Blog
IT之家
IT之家
Security Archives - TechRepublic
Security Archives - TechRepublic
阮一峰的网络日志
阮一峰的网络日志
N
News and Events Feed by Topic
GbyAI
GbyAI
B
Blog
O
OpenAI News
博客园_首页
Cisco Talos Blog
Cisco Talos Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Hacker News: Ask HN
Hacker News: Ask HN
TaoSecurity Blog
TaoSecurity Blog
腾讯CDC
MongoDB | Blog
MongoDB | Blog
M
MIT News - Artificial intelligence
C
Cybersecurity and Infrastructure Security Agency CISA
Cyberwarzone
Cyberwarzone
Webroot Blog
Webroot Blog
Simon Willison's Weblog
Simon Willison's Weblog
Y
Y Combinator Blog
C
Cisco Blogs
A
Arctic Wolf
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
T
The Exploit Database - CXSecurity.com
Security Latest
Security Latest
AI
AI
W
WeLiveSecurity
aimingoo的专栏
aimingoo的专栏
The Register - Security
The Register - Security
Project Zero
Project Zero
H
Hackread – Cybersecurity News, Data Breaches, AI and More
N
Netflix TechBlog - Medium
Blog — PlanetScale
Blog — PlanetScale

博客园 - s1ihome

弹框居中的方法 如何 clone git 项目到一个非空目录 Uiautomatorviewer报错:Unexpected error while obtaining UI hierarchy java.lang.reflect.InvocationTargetException 常见银行卡号账号长度参考表 TP5.0 数据库查询is not null vscode 中sftp配置 Thinkphp关联模型使用 centos 7 pdo thinkphp 5 where 组合条件map数组or [转]处理上百万条的数据库如何提高处理查询速度 连接oracle读取数据 SQL SERVER批量修改表名前缀 php导出excel java开发微信公众平台备忘 AngularJS小试牛刀 spring mvc开发过程知识点记录 微信公众平台项目中遇到的小问题40016,Invalid button size Dynamic Virtual Channels 木马的隐藏方式
移动端H5上传图片并压缩上传
s1ihome · 2017-10-11 · via 博客园 - s1ihome

手头上的这个项目主要是在微信内运行的一个网站,需要用户上传手机内的照片,而现在手机照片尺寸越来越大,直接上传的话的确上传进度慢影响用户体验而且也会给服务器增加压力,所以利用H5的新特性压缩后上传不失为一良策。

最后选用的是localResizeImg进行压缩上传,简单易上手,核心代码部分如下。

前台部分

<input id="upload" type="file" name="upload" multiple accept="image/*;capture=camcorder">

js部分:

var photoIdx=0;

document.querySelector('#upload').addEventListener('change', function () {

  

var filesCount = this.files.length;

  

if(filesCount>18) return zalert('最多只能上传18张照片');


  

var uploadedCount = $('.img-preview-box img').size();

  

if(uploadedCount>17) return zalert('最多只能上传18张照片');

  

var imageType = /^image\//;

  

var container = $('.img-preview-box');


  

if(filesCount>0 && imageType.test(this.files[0].type)) container.css({'min-height':'110px','overflow':'visible'})

  

for(var i=0,file;i<filesCount,file=this.files[i];i++){

    

if (!imageType.test(file.type)) { return zalert("上传的图片格式不正确,请重新选择")}

    

lrz(file).then(function (rst) {

      

container.append('<div class="uploadimg-box"><img src="'+ rst.base64 +'"/><div class="del-uploadimg-box" title="移除该照片" data-id="'+ photoIdx +'"></div></div>');

}).then(function (rst) {

      

rst.formData.append('fileLen', rst.fileLen);

          

success: function (data) {

            

var photos=$('#uploadedPhotos').val();

            

$('#uploadedPhotos').val(photos+','+ data.url);             

          }

      });   

    }).catch(function (err) {}).always(function () {});

  }

});

PHP后台部分:

    public function upload() 
    {
        $config = [
            'size' => 2097152,
            'ext'  => 'jpg,gif,png,bmp'
        ];

        $file = $this->request->file('file');

        $upload_path = str_replace('\\', '/', ROOT_PATH . 'public/uploads/photos');
        $save_path   = '/public/uploads/photos/';
        $info        = $file->validate($config)->move($upload_path);

        if ($info) {
            $result = [
                'error' => 0,
                'url'   => str_replace('\\', '/', $save_path . $info->getSaveName())
            ];
        } else {
            $result = [
                'error'   => 1,
                'msg' => $file->getError()
            ];
        }

        return json($result);
    }