Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/248.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 通过ajax上传,但php返回的$\u文件为空_Javascript_Php_Ajax - Fatal编程技术网

Javascript 通过ajax上传,但php返回的$\u文件为空

Javascript 通过ajax上传,但php返回的$\u文件为空,javascript,php,ajax,Javascript,Php,Ajax,我希望通过ajax上传img,我的php无法接收我的ajax帖子 function handleFileSelect(evt) { files = evt.target.files; // FileList object $('.thumb-canvas' + filesId).css('display','block'); // Loop through the FileList and render image files as thumbnails. f

我希望通过ajax上传img,我的php无法接收我的ajax帖子

function handleFileSelect(evt) {
    files = evt.target.files; // FileList object

    $('.thumb-canvas' + filesId).css('display','block');

    // Loop through the FileList and render image files as thumbnails.
    for (var i = 0, f; f = files[i]; i++) {

      // Only process image files.
      if (!f.type.match('image.*')) {
        continue;
      }

      var reader = new FileReader();

      // Closure to capture the file information.
      reader.onload = (function(theFile) {
        return function(e) {
          // Render thumbnail.
          var span = document.createElement('span');
          span.innerHTML = ['<img class="thumb" src="', e.target.result,
                            '" title="', escape(theFile.name), '"/>'].join('');
          document.getElementById('list' + filesId).insertBefore(span, null);
        };
      })(f);

      // Read in the image file as a data URL.
      reader.readAsDataURL(f);

      //upload ajax

  var xhr = new XMLHttpRequest();

  var formData = new FormData();

  formData.append('file',files);

  xhr.upload.addEventListener("loadstart", function(e){

    $("#progressbar").show();

    $("#percentage").show();

  }, false);


  xhr.open("POST", "upload.php");

  xhr.overrideMimeType('text/plain; charset=x-user-defined-binary');

  xhr.send(formData);



    }
  }
我得到空值。我还尝试了echo var_dump$_FILES['FILES']

我尝试使用$\u POST检查是否有内容,是的,它不是空的,但我想知道为什么我不能使用$\u文件

'text/plain; charset=x-user-defined-binary'
这不是文件传输后的数据编码,因此PHP无法找到文件,因为数据必须是多部分表单数据


您可以使脚本正确发送,然后PHP会将文件查找到$\u文件中。或者您可以像这里这样进行自定义数据传输

xhr.overrideMimeType'text/plain;字符集=x-用户定义的二进制';我从来没有在上传时使用过AJAX,但这不是错误的mimetype吗?我认为您需要一些类似于普通HTML的多部分/表单数据?我不确定,可能是另一个错误,但文件不是文本/普通文件。$\u文件中没有元素文件。尝试转储整个超全局文件:var\u dump$\u文件;
'text/plain; charset=x-user-defined-binary'