Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 上传及;使用JIC库压缩图像,只需单击一次_Javascript_Jquery_Image Compression - Fatal编程技术网

Javascript 上传及;使用JIC库压缩图像,只需单击一次

Javascript 上传及;使用JIC库压缩图像,只需单击一次,javascript,jquery,image-compression,Javascript,Jquery,Image Compression,我希望你们每个人都身体健康 我正在开发JIC库,用于在图像传递到后端之前压缩客户端的图像。我不知道如何使用它 但我不知道如何在1个按钮单击功能中添加它 有许多演示可供选择的图像,压缩图像,然后上传图像。但我希望所有这些都在一个函数中 我尝试使用async和await来使用它,但它对我来说并不好用 <label for="file">Choose file</label> <input type="file" id="

我希望你们每个人都身体健康

我正在开发JIC库,用于在图像传递到后端之前压缩客户端的图像。我不知道如何使用它

但我不知道如何在1个按钮单击功能中添加它

有许多演示可供选择的图像,压缩图像,然后上传图像。但我希望所有这些都在一个函数中

我尝试使用asyncawait来使用它,但它对我来说并不好用

<label for="file">Choose file</label>
<input type="file" id="fileinput" />
<img id="source_image"  style="display: none;">
<img id="compressed_image"  style="display: none;">

<script>
var output_format = null;
var file_name = null;
var i = document.getElementById("source_image");
        
async function change_file(evt)
{
    let res = await readFile_2(evt); // waits for readFile to complete before moving to the next line
    let res2 = await compress(res); // similarly waits for the completion of readFile_2
    let res3 = await upload(res2); // similarly waits for the completion of compress
    console.log(res3)
    console.log("end of change_file")
}

function readFile_2(evt)
{
    var file = evt.target.files[0];
    var reader = new FileReader();
    
    /*
    reader.onload = function(event) {
        console.log(i);
            i.src = event.target.result;
            i.onload = function(){
                
                console.log("Image loaded");
            }
    };
    */
    
    reader.addEventListener("load", function ()
    {
        i.src = reader.result;
    }, false);
    if (file)
    {
        reader.readAsDataURL(file);
    }
    
    output_format = file.name.split(".").pop();
    file_name = file.name;
    console.log("Filename:" + file.name);
    console.log("Fileformat:" + output_format);
    console.log("Filesize:" + (parseInt(file.size) / 1024) + " Kb");
    console.log("Type:" + file.type);
    reader.readAsDataURL(file);
    // return false;
    return [1];
}

function compress()
{
    var source_image = document.getElementById("source_image");
    if (source_image.src == "") {
        console.log("You must load an image first!");
        return false;
    }

    var quality = 30;
    
    console.log("process start...");
    console.log("process start compress ...");
    compressed_image.src = jic.compress(source_image,quality,output_format).src;
    return [1];
}

function upload()
{
    var compressed_image = document.getElementById("compressed_image");
    if (compressed_image.src == "") {
        console.log("You must compress image first!");
        return false;
    }

    var successCallback= function(response){
        console.log("image uploaded successfully! :)");
        console.log(response);       
    }

    var errorCallback= function(response){
        console.log("image Filed to upload! :)");
        console.log(response); 
    }
    
    console.log("process start upload ...");
    jic.upload(compressed_image, "upload.php", "file", file_name,successCallback,errorCallback);
    return [1];
}

document.getElementById("fileinput").addEventListener("change", change_file, false);
</script>
选择文件
想要:请帮我解决这个问题。唯一的问题是JS继续运行下一个代码,而不是等待上一个代码

注意:请不要给我有关承诺等的例子。我尝试了所有,但没有为我工作