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

推荐订阅源

博客园 - 叶小钗
云风的 BLOG
云风的 BLOG
G
Google Developers Blog
S
SegmentFault 最新的问题
罗磊的独立博客
Hugging Face - Blog
Hugging Face - Blog
美团技术团队
爱范儿
爱范儿
博客园 - 三生石上(FineUI控件)
H
Hackread – Cybersecurity News, Data Breaches, AI and More
D
DataBreaches.Net
F
Fortinet All Blogs
TaoSecurity Blog
TaoSecurity Blog
D
Docker
C
Cybersecurity and Infrastructure Security Agency CISA
K
Kaspersky official blog
宝玉的分享
宝玉的分享
腾讯CDC
Google Online Security Blog
Google Online Security Blog
Recorded Future
Recorded Future
T
The Exploit Database - CXSecurity.com
T
The Blog of Author Tim Ferriss
V
V2EX
S
Securelist
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
C
CERT Recently Published Vulnerability Notes
A
Arctic Wolf
Scott Helme
Scott Helme
L
LINUX DO - 热门话题
Y
Y Combinator Blog
P
Proofpoint News Feed
T
Tor Project blog
AWS News Blog
AWS News Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
The Last Watchdog
The Last Watchdog
博客园 - 聂微东
T
Threat Research - Cisco Blogs
B
Blog
Attack and Defense Labs
Attack and Defense Labs
L
Lohrmann on Cybersecurity
C
CXSECURITY Database RSS Feed - CXSecurity.com
阮一峰的网络日志
阮一峰的网络日志
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
IT之家
IT之家
N
News and Events Feed by Topic
博客园 - 司徒正美
H
Help Net Security
C
Cisco Blogs
C
Check Point Blog
S
Secure Thoughts

博客园 - 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);
    }