在客户端上传许多文件并压缩它然后在服务器上上传压缩文件 [英] Upload many file on client side and compress it then upload compress file on server

查看:122
本文介绍了在客户端上传许多文件并压缩它然后在服务器上上传压缩文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现概念网站访问者可以上传多个文件点击提交然后压缩文件上传服务器xampp。我正在使用PHP脚本语言。

I want to implement the concept site visitor can upload multiple files click on submit then compress files are upload on server xampp. I am using PHP scripting language.

推荐答案

您可以在Canvas API的帮助下在HTML5支持的浏览器中执行此操作[仅适用于图像]。这是一个很好的例子

You can do this in HTML5 supported browser with the help of Canvas API [for images only]. Here is a good example

http:// makeitsolutions。 com / labs / jic /

HTML5画布参考:

HTML5 canvas refrences:

http://diveintohtml5.info/canvas.html

http://www.html5canvastutorials.com/

以下是虚拟代码:

HTML [检查jQuery路径]

HTML [Check jQuery path]

    <!DOCTYPE html>
<html>
    <head>
        <title></title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <script src="http://code.jquery.com/jquery-1.10.2.min.js" type="text/javascript"></script>
        <style type="text/css">
            .img_button{ height: 100%; width:100%}
            .img_submit{ border: 1px saddlebrown solid; height: 30px; margin-top: 100px}
            .box{ float: left; margin: 10px; width: 20%; height: 250px}
            .label{ float: left; background: #333; color: #fff; width: 100%; padding-left: 10px }
            img{float:left;}
        </style>
    </head>
    <body>
        <div class="box" id="box1">
            <input class="filename" type="file" id="1" style="display:none" />
            <input class="img_button" id="btn1" type="button" onclick="$('#1').trigger('click'); return false;" value="Image-1" />
        </div>
        <div class="box" id="box2">
            <input class="filename" type="file" id="2" style="display:none" />
            <input class="img_button" id="btn2" type="button" onclick="$('#2').trigger('click'); return false;" value="Image-2" />
        </div>
        <div class="box" id="box3">
            <input class="filename" type="file" id="3" style="display:none" />
            <input class="img_button" id="btn3" type="button" onclick="$('#3').trigger('click'); return false;" value="Image-3" />
        </div>
        <input class="img_submit" type="button" value="Upload" onclick="uploadFile();" />
        <script type="text/javascript">
            var imgCount = 1;
            $('.filename').change(function(){
                var file = this.files[0];
                var id = this.id;
                var reader = new FileReader();
                reader.onload = function(event) {
                    var i = document.createElement('img');
                    i.src = event.target.result;
                    i.id = 'img'+id;
                    i.onload = function(){
                        image_width=$(i).width(),
                        image_height=$(i).height();

                        if(image_width > image_height){
                            i.style.width="320px";
                        }else{
                            i.style.height="300px";
                        }
                        //i.style.display = "block";    
                    }
                    $('#img'+id).remove();
                    $('#box'+id).append(i);
                    $('#box'+id).prepend('<span class="label">'+$('#btn'+id).val()+'</span>');
                    $('#btn'+id).hide();
                    $(document).on('click', '#img'+id, function(){$('#'+id).trigger('click')});
                };
                reader.onerror = function(event) {
                    console.error("File could not be read! Code " + event.target.error.code);
                };
                reader.readAsDataURL(file);
            });

            function uploadFile(){

                var img_data = [];

                if(imgCount){
                    var quality = 0.3;
                    for(var i=1; i<=3; i++){
                        if(document.getElementById('img'+i)){
                            var source_img_obj = document.getElementById('img'+i);
                            var cvs = document.createElement('canvas');
                            cvs.width = source_img_obj.naturalWidth;
                            cvs.height = source_img_obj.naturalHeight;
                            var ctx = cvs.getContext("2d").drawImage(source_img_obj, 0, 0);
                            var newImageData = cvs.toDataURL("image/jpeg", quality);
                            var img_name = $('#btn'+i).val();
                            img_data.push({index:i, name: img_name, image_data :newImageData});
                        }
                    }

                    var xhr = $.ajax({
                        url: 'a.php',
                        type: 'POST',
                        data: {post_data:img_data},
                        dataType: 'json'
                    });

                    xhr.success(function(response){
                        console.log(response);
                    });
                }
            }
        </script>
    </body>
</html>

PHP

<?php
$arr = $_POST;
if(isset($arr) && isset($arr['post_data'])){
    $arrImageData = $arr['post_data'];
    if(is_array($arrImageData)){
        for($i=0; $i<count($arrImageData); $i++){
            if($arrImageData[$i]['image_data'] != ''){
                $varImageData = preg_replace('/^data:image\/(png|jpg|jpeg);base64,/', '', $arrImageData[$i]['image_data']);
                $varImageData = base64_decode($varImageData);
                $varImageIndex = $arrImageData[$i]['index'];
                $varImageName = preg_replace('/[^a-zA-Z0-9]/', '-', $arrImageData[$i]['name']);
                $varFileName = $varImageName.'-'.$varImageIndex.'.jpg';             

                $file = fopen($varFileName, 'wb');
                fwrite($file, $varImageData);
                fclose($file);
            }
        }
    }
}

这篇关于在客户端上传许多文件并压缩它然后在服务器上上传压缩文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆