使用ajax post将图像从localstore发送到mysql

时间:2022-05-19 22:10:23

I'm trying to send a picture stored in localstore with javascript but can't retrieve it and show it.

我正在尝试使用javascript发送存储在localstore中的图片,但无法检索并显示它。

Javascript part :

Javascript部分:

liste['pic'] = localStorage['pic'];

            $.ajax({
                type: "POST",  
                url: "save.php",  
                data: { pic : liste['pic'] },
                dataType: "json", 
                success: function(data) {
                    if(data) {
                        alert("Picture sent succesfully");
                    }
                }  
                });

The php part that receive the data :

接收数据的php部分:

require "lib/connect.php";
$pic = $_POST['pic'];
$insert_query = "INSERT INTO liste ( `pic` ) VALUES ( '".$pic."' );";
$result = mysql_query($insert_query);

The php part that shows the pic. There's something in the table but since it's blob , I can't check if the right data.

显示图片的php部分。表中有一些东西,但由于它是blob,我无法检查是否有正确的数据。

$select_query = "Select   `pic` From liste;";
$result = $dbhandle->query($select_query);

echo "<table border='1'>
<tr>
<th>Image</th>
</tr>";

while($row = mysqli_fetch_array($result))
  {
  echo "<tr>";
  echo "<td><img src=\"" . $row['pic'] . "\" width=\"200\"/><br/><br/></td>";
  echo "</tr>";
  }
echo "</table>";
$result->closeCursor();
mysqli_close($dbhandle);

from this I get a broken image. What is missing ? Text works but not image, why ?

从此我得到一个破碎的图像。什么不见​​了 ?文字有效但不是图像,为什么?

1 个解决方案

#1


0  

What you need to know is that when you are sending values encoded in json through POST, these values are not present in the $_POST variables.

您需要知道的是,当您通过POST发送在json中编码的值时,这些值不会出现在$ _POST变量中。

The only way to have values in the POST variables is by using having the application/x-www-form-urlencoded and multipart/form-data.

在POST变量中获取值的唯一方法是使用application / x-www-form-urlencoded和multipart / form-data。

If you wish to use another content-type, you actually need to use 'php://input'.

如果你想使用其他内容类型,你实际上需要使用'php:// input'。

e.g.

$data = json_decode(file_get_contents('php://input'), true);
$text = print_r($data, true);

You should then see an array containing your image's data.

然后,您应该看到一个包含图像数据的数组。

#1


0  

What you need to know is that when you are sending values encoded in json through POST, these values are not present in the $_POST variables.

您需要知道的是,当您通过POST发送在json中编码的值时,这些值不会出现在$ _POST变量中。

The only way to have values in the POST variables is by using having the application/x-www-form-urlencoded and multipart/form-data.

在POST变量中获取值的唯一方法是使用application / x-www-form-urlencoded和multipart / form-data。

If you wish to use another content-type, you actually need to use 'php://input'.

如果你想使用其他内容类型,你实际上需要使用'php:// input'。

e.g.

$data = json_decode(file_get_contents('php://input'), true);
$text = print_r($data, true);

You should then see an array containing your image's data.

然后,您应该看到一个包含图像数据的数组。