如何将使用 jquery 上传的图像数据传输到 php


How to transfer the image data uploaded using jquery to php?

我这里有一个代码

  <h2>Click blue button</h2>
  <button id="open_btn" class="btn btn-primary">Open dialog</button>
  <div id="output"></div>
  <script src="src/bootstrap.fd.js"></script>
  <script type="text/javascript">
    $("#open_btn").click(function() {
      $.FileDialog({multiple: true}).on('files.bs.filedialog', function(ev) {
        var files = ev.files;
        var text  = "";
        files.forEach(function(f) {
          text += f.name + "<br/>";
        });
        $("#output").html(text);
      }).on('cancel.bs.filedialog', function(ev) {
        $("#output").html("Cancelled!");
      });
    });
  </script>

这是一个使用 http://www.jqueryscript.net/demo/Drag-Drop-File-Upload-Dialog-with-jQuery-Bootstrap 的jQuery和Boostrap布局的拖放上传。它可以工作,但问题是,我不知道如何将上传到php的数据传递给php进行处理并将文件放入服务器。

有人可以帮助我吗?

你可以尝试使用ajax。从网络上提供的大量教程中在您的项目中设置 ajax。之后在你的javascript函数中,我想你想将变量text传递给php。因此,当您通过教程了解ajax时,您可以执行以下操作

 <script type="text/javascript">
        $("#open_btn").click(function() {
          $.FileDialog({multiple: true}).on('files.bs.filedialog', function(ev)                {
            var files = ev.files;
            var text  = "";
            files.forEach(function(f) {
              text += f.name + "<br/>";
    });    
    $.ajax({
                url: "'your php function name'?name ="+text,
                success: function( data ) {
                    if(data == "retn value") { //return value of the php function
    //                  alert("");
                    } else {
                    }
                }
            });
            $("#output").html(text);
          }).on('cancel.bs.filedialog', function(ev) {
            $("#output").html("Cancelled!");
          });
        });
      </script>

在 PHP 中创建一个函数,它将您的数据作为参数。希望这有帮助。