简体   繁体   English

在客户端上载许多文件并进行压缩,然后在服务器上载压缩文件

[英]Upload many file on client side and compress it then upload compress file on server

I want to implement the concept site visitor can upload multiple files click on submit then compress files are upload on server xampp. 我想实现概念,网站访问者可以上传多个文件,然后单击提交,然后将压缩文件上传到服务器xampp。 I am using PHP scripting language. 我正在使用PHP脚本语言。

You can do this in HTML5 supported browser with the help of Canvas API [for images only]. 您可以借助Canvas API在HTML5支持的浏览器中进行此操作(仅适用于图像)。 Here is a good example 这是一个很好的例子

http://makeitsolutions.com/labs/jic/ http://makeitsolutions.com/labs/jic/

HTML5 canvas refrences: HTML5画布引用:

http://diveintohtml5.info/canvas.html http://diveintohtml5.info/canvas.html

http://www.html5canvastutorials.com/ http://www.html5canvastutorials.com/

Below is dummy code: 下面是伪代码:

HTML [Check jQuery path] HTML [检查jQuery路径]

    <!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

<?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);
            }
        }
    }
}

Client-side compression (before upload) can only be done via a Java Applet, as far as I know. 据我所知,客户端压缩(在上传之前)只能通过Java Applet完成。

Server-side compression (after upload) can be done via PHP ZipArchive class. 服务器端压缩(上传后)可以通过PHP ZipArchive类完成。 An example can be found here . 一个例子可以在这里找到。

EDIT: In addition to Java Applets, client-side file compression can also be implemented in Flash or Silverlight, but if I understand correctly, this will compress data per file for quicker sending and not create a file archive. 编辑:除了Java Applets,客户端文件压缩也可以在Flash或Silverlight中实现,但是如果我理解正确,这将压缩每个文件的数据以加快发送速度,而不创建文件存档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在使用jquery上传之前在客户端压缩文件(例如pdf)? - How to compress the file(such as pdf) in client side before upload using jquery? 压缩文件(客户端)并上传 - Compress files (client side) and upload 在客户端(angularjs)压缩图像并上传到服务器(nodejs) - Compress image at client side (angularjs) and upload to server(nodejs) 大JSON文件-如何在服务器中压缩和在客户端解压缩? - Big JSON file - how to compress in the server and decompress in the client side? 在 javascript 中上传之前压缩选定的视频文件 - compress selected video file before upload in javascript 在使用 JIC 库上传到服务器之前从客户端压缩多个图像 - compress multiple images from client side before upload to the server using JIC library 在强大的上传中使用 compress_image 时“文件不存在” - “File does not exist” when using compress_image on formidable upload 在React(客户端)中上传.json文件并将其发送到Node(服务器端) - Upload .json file in React(client side) and send it to Node(server side) GWT客户端文件上传 - GWT client side file upload 在服务器上压缩JSON消息并在客户端解压缩 - Compress JSON message on server and decompress in client side
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM