如何使用SQLite插件插入图像?

时间:2015-12-09 07:44:57

标签: jquery sqlite cordova

您好我正在尝试将图像插入sqlite数据库。我正在使用cordova,html和jquery。我尝试使用带有readAsUrl的filereader读取图像,这给了我base64格式的图像。我对存储这张图片感到震惊。 这段代码工作正常,

var file  = document.querySelector('input[type=file]').files[0];

  var reader  = new FileReader();


  reader.onloadend = function () {
img = reader.result;
    if(file.type.match('image.*'))
    {
    img = reader.result;
    console.log(img);
}
else
{
  alert("select an image file");
}
  }

  if (file) {
    reader.readAsBinaryString(file);
    var objectURL = URL.createObjectURL(reader.result);
imagess = objectURL;
console.log(objectURL);

  } else {
    preview.src = "";
  }

使用上面的代码,我可以将图像作为base64格式,当我将其设置为图像源时,我可以在浏览器中看到图像。有人可以帮帮我吗?

1 个答案:

答案 0 :(得分:0)

使用PHP move_uploaded_filemove_uploaded_file        它将图像存储在localhost中的临时位置          所以..在localhost中创建一个文件夹来存储上传的图像

 <?php

    if(isset($_POST['submit'])){

     global $conn;

   $image_tmp = $_FILES['image']['tmp_name'];   
   $image = $_FILES['image']['name'];

    if($image == '' ){

   echo "<script>alert('please upload an image')</script>";



    } else{

   move_uploaded_file($image_tmp,"images/$image");

   $insert  = "insert into tablename (image) values ('$image')";

   $run = mysqli_query($conn,$insert);

     echo "data inserted successfully";

       }

     }


 ?> 


and to retrieve the stored image

 <?php

 global $conn;


 $viewfiles = "SELECT * FROM tablename ORDER BY RAND() LIMIT 0,5";

      $view = mysqli_query($conn,$viewfiles);


   while($row = mysqli_fetch_array($view)){


    $var1 = $row['image'];




   echo "
  <div class='box'>

  <h2><center>$mainname</center></h2><br>

   <p>$maincomments</p><br>

   <img src = 'images/$var1' width = '120' height = '120' />

    </div><br><br>

         ";

            }

        ?>