在数据库中不使用JQuery存储图像

时间:2022-02-10 16:38:16

Good day stackers,

堆垛机的好日子,

I'm working on a web project in which we recreate a social media site similar to snapchat. I'm using my webcam to take pictures using JS, and I'm writing the picture to a var called img as follows:

我正在做一个网络项目,在这个项目中,我们重新创建了一个类似于snapchat的社交媒体网站。我用我的摄像头用JS拍照,我把照片写在一个叫img的var上,如下所示:

var img    = canvas.toDataURL("image/png");

We are required to use PDO in PHP to access the database. Alternatively we can use AJAX, but JQuery is strictly forbidden. My question is, how can I store the DataURL inside my database? All the tutorials online use JQuery.

我们需要在PHP中使用PDO来访问数据库。我们也可以使用AJAX,但是JQuery是被严格禁止的。我的问题是,如何在数据库中存储DataURL ?所有的教程都使用JQuery。

Update:

更新:

I followed the steps as suggested below, but when I hit the snap button, it still only takes the picture, but no URL or nothing.

我按照下面建议的步骤,但是当我按下snap按钮时,它仍然只会拍照,没有URL或什么都没有。

function sendimagetourl(image)
        {
            var xhttp = new XMLHttpRequest();
            xhttp.onreadystatechange = function()
            {
                if (this.readyState == 4 && this.status == 200)
                {
                    alert( this.resoponseText);
                }
            }
            xhtp.open("GET", "saveimage.php?url="+image, true);
            xhttp.send();
        }
        //Stream Video
        if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia)
        {
            navigator.mediaDevices.getUserMedia({video: true}).then(function(stream) {
            video.src = window.URL.createObjectURL(stream);
            video.play();
            });
        }
        //Snap Photo
        var canvas = document.getElementById('canvas');
        var context = canvas.getContext('2d');
        var video = document.getElementById('video');

        document.getElementById("snap").addEventListener("click", function() {context.drawImage(video, 0, 0, 800, 600);var image = canvas.toDataURL("image/png"); sendimagetourl(image);});

So it appears I was a bit of a fool. There were two minor typos, and it seems like the data sent is invisible in the url. Thanks for all the help!

看来我有点傻。有两个小错误,看起来发送的数据在url中是不可见的。谢谢你的帮助!

2 个解决方案

#1


1  

You can use javascript ajax

可以使用javascript ajax

var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
  if (this.readyState == 4 && this.status == 200) {
    alert( this.responseText);
  }
};
xhttp.open("GET", "saveimage.php?url="+url, true);
xhttp.send();

#2


1  

You can use XMLHttpRequest() or fetch(), Blob, FormData to POST data URI to php. See also Using files from web applications

您可以使用XMLHttpRequest()或fetch()、Blob、FormData将数据URI发送到php。请参见使用来自web应用程序的文件

  var request = new XMLHttpRequest();
  request.open("POST", "/path/to/server", true);
  request.onload = function() {
    // do stuff
  }
  var data = new FormData();
  data.append("image", new Blob([img], {type:"image/png"}), "filename");
  request.send(data);

#1


1  

You can use javascript ajax

可以使用javascript ajax

var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
  if (this.readyState == 4 && this.status == 200) {
    alert( this.responseText);
  }
};
xhttp.open("GET", "saveimage.php?url="+url, true);
xhttp.send();

#2


1  

You can use XMLHttpRequest() or fetch(), Blob, FormData to POST data URI to php. See also Using files from web applications

您可以使用XMLHttpRequest()或fetch()、Blob、FormData将数据URI发送到php。请参见使用来自web应用程序的文件

  var request = new XMLHttpRequest();
  request.open("POST", "/path/to/server", true);
  request.onload = function() {
    // do stuff
  }
  var data = new FormData();
  data.append("image", new Blob([img], {type:"image/png"}), "filename");
  request.send(data);