简体   繁体   English

无法下载base64解码的图像

[英]base64 decoded image does not download

I am using the following scripts to convert html canvas to png and let the client download it without saving the image to server. 我正在使用以下脚本将html canvas转换为png,并让客户端下载它而不将图像保存到服务器。 These scripts worked in xampp and also in the server cuccfree.cc. 这些脚本在xampp和服务器cuccfree.cc中均有效。 But the same scripts doesn't work in the server that i need it to be hosted. 但是相同的脚本无法在服务器中工作,而我需要将其托管。

Javascript Java脚本

         fname = fname || 'picture';

        var data = cnvs.toDataURL("image/png");
        data = data.substr(data.indexOf(',') + 1).toString();

        var dataInput = document.createElement("input") ;
        dataInput.setAttribute("name", 'imgdata') ;
        dataInput.setAttribute("value", data);
        dataInput.setAttribute("type", "hidden");

        var nameInput = document.createElement("input") ;
        nameInput.setAttribute("name", 'name') ;
        nameInput.setAttribute("value", fname + '.png');

        var myForm = document.createElement("form");
        myForm.method = 'post';
        myForm.action = url;
        myForm.appendChild(dataInput);
        myForm.appendChild(nameInput);

        document.body.appendChild(myForm) ;
        myForm.submit() ;
        document.body.removeChild(myForm) ;

PHP scipt PHP Scipt

<?php
    # we are a PNG image
    header('Content-type: image/png');

    # we are an attachment (eg download), and we have a name
    header('Content-Disposition: attachment; filename="' . $_POST['name'] .'"');

    #capture, replace any spaces w/ plusses, and decode
    $encoded = $_POST['imgdata'];
    $encoded = str_replace(' ', '+', $encoded);
    $decoded = base64_decode($encoded);

    #write decoded data
    echo $decoded;
?>

but this is the output i get when i clicked on the button which is assigned to the javascript function. 但这是我单击分配给javascript函数的按钮时得到的输出。 http://oi57.tinypic.com/2je1cva.jpg http://oi57.tinypic.com/2je1cva.jpg

How can i correct this error??? 我该如何纠正此错误? please help me.... 请帮我....

I suggest you to check the php extensions in php.ini in the different servers you are working with. 我建议您在正在使用的不同服务器上检查php.ini中的php扩展名。 May be an extension is missing in your production server. 可能是生产服务器中缺少扩展名。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM