Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/291.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
使用Ajax和Javascript保存base64图像_Javascript_Php_Jquery_Ajax_Base64 - Fatal编程技术网

使用Ajax和Javascript保存base64图像

使用Ajax和Javascript保存base64图像,javascript,php,jquery,ajax,base64,Javascript,Php,Jquery,Ajax,Base64,我正在尝试通过ajax和FileReader使用base64解码保存/上载图像。照片正在显示,但未在指定目的地保存。有人能帮忙吗 守则: HTML JS 函数readImage(文件){ var reader=new FileReader(); var image=新图像(); reader.readAsDataURL(文件); reader.onload=函数(\u文件){ image.src=\u file.target.result; image.onload=函数(){ var w=

我正在尝试通过ajax和FileReader使用base64解码保存/上载图像。照片正在显示,但未在指定目的地保存。有人能帮忙吗

守则:

HTML


JS

函数readImage(文件){
var reader=new FileReader();
var image=新图像();
reader.readAsDataURL(文件);
reader.onload=函数(\u文件){
image.src=\u file.target.result;
image.onload=函数(){
var w=此宽度,
h=这个高度,
t=file.type,
n=file.name,
s=~~(file.size/1024)+'KB';
$('#uploadPreview')。追加('
'); }; image.onerror=函数(){ 警报('无效文件类型:'+file.type); }; }; } $(“#选择”)。更改(函数(e){ if(this.disabled)返回警报('不支持文件上载!'); var F=this.files; 如果(F&&F[0])用于(var i=0;i

Thx寻求帮助。

获取数据并(假定)保存数据的(PHP)代码是什么?我已经回答了我的问题
<input type="file" id="choose" multiple="multiple" />
function readImage(file) {

    var reader = new FileReader();
    var image  = new Image();

    reader.readAsDataURL(file);
    reader.onload = function(_file) {
        image.src    = _file.target.result;
        image.onload = function() {
            var w = this.width,
                h = this.height,
                t = file.type,
                n = file.name,
                s = ~~(file.size/1024) +'KB';
            $('#uploadPreview').append('<img src="'+ this.src +'"><br>');
        };
        image.onerror= function() {
            alert('Invalid file type: '+ file.type);
        };
    };

}
$("#choose").change(function (e) {
    if (this.disabled) return alert('File upload not supported!');
    var F = this.files;
    if (F && F[0]) for(var i=0; i<F.length; i++) readImage( F[i] );
    $.each(this.files, function(index, file) {
        $.ajax({
            url: "upload.php",
            type: 'POST',
            data: {filename: file.filename, data: file.data},
            success: function(data, status, xhr) {}
        });
    });
    files = [];
});
<?php
function uploadimg() {

    $error = false;
    //if ( ! function_exists( 'wp_handle_upload' ) ) require_once( ABSPATH . 'wp-admin/includes/file.php' );
    //$upload_overrides = array( 'test_form' => false );
    $images=array();

    $a=0;
    unset ($_POST['action']);

    foreach($_POST as $basefile){


        $upload_dir       = wp_upload_dir();
        $upload_path      = str_replace( '/', DIRECTORY_SEPARATOR, $upload_dir['path'] ) . DIRECTORY_SEPARATOR;
        $base64_string = $basefile;
        echo $basefile;
        $base64_string = preg_replace( '/data:image\/.*;base64,/', '', $base64_string );
        $decoded_string= base64_decode($base64_string); // 0 bytes written in fwrite
        $source = imagecreatefromstring($decoded_string); // 0 bytes written in fwrite
        $output_file = $upload_path.'myfilef'.$a.'.jpg';
        $images[]=$output_file;
        $a++;
        $image = fopen( $output_file, 'wb' );

        $bytes=fwrite( $image, $source);
        echo $bytes;
        fclose( $image );
    }
    echo json_encode($images);
    exit;

}

add_action('wp_ajax_uploadimg', 'uploadimg');
add_action('wp_ajax_nopriv_uploadimg', 'uploadimg');
?>