将`toDataURL`图像保存到服务器

时间:2017-06-17 06:12:36

标签: javascript ajax post todataurl

我已尝试实施signature_pad,但老实说我不明白。

如何返回图像值并将其发布到我的服务器?

编辑:

我尝试过解码:

JavaScript“app.js”:

saveButton.addEventListener("click", function (event) {
    if (signaturePad.isEmpty()) {
        alert("Please provide signature first.");
    } else {
        saveSignature(signaturePad.toDataURL());
    }
});

function saveSignature(dataURL) {
$.ajax({
  type: "POST",
  url: "script.php",
  data: { 
     imgBase64: dataURL
  }
}).done(function(o) {
  console.log('saved'); 
});
}

PHP,“script.php”:

<?php
    // requires php5
    define('UPLOAD_DIR', 'images/');
    $img = $_POST['img'];
    $img = str_replace('data:image/png;base64,', '', $img);
    $img = str_replace(' ', '+', $img);
    $data = base64_decode($img);
    $file = UPLOAD_DIR . uniqid() . '.png';
    $success = file_put_contents($file, $data);
    print $success ? $file : 'Unable to save the file.';
?>

1 个答案:

答案 0 :(得分:1)

在PHP代码中,您必须获得# Importing the libraries import numpy as np import matplotlib.pyplot as plt import pandas as pd # Importing the dataset dataset = pd.read_csv('Data1.csv',encoding = "cp1252") X = dataset.values # Encoding categorical data from sklearn.preprocessing import LabelEncoder, OneHotEncoder labelencoder_X_0 = LabelEncoder() X[:, 0] = labelencoder_X_0.fit_transform(X[:, 0]) labelencoder_X_1 = LabelEncoder() X[:, 1] = labelencoder_X_1.fit_transform(X[:, 1]) labelencoder_X_2 = LabelEncoder() X[:, 2] = labelencoder_X_2.fit_transform(X[:, 2]) onehotencoder = OneHotEncoder(categorical_features = "all") X = onehotencoder.fit_transform(X).toarray() 而不是$_POST['imgBase64']

  

您的PHP代码不会返回$_POST['img']数据

试试这个: json