Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/287.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 访问类型=";文件";来自服务器端POST方法的输入_Javascript_Php_Html_Ajax_Post - Fatal编程技术网

Javascript 访问类型=";文件";来自服务器端POST方法的输入

Javascript 访问类型=";文件";来自服务器端POST方法的输入,javascript,php,html,ajax,post,Javascript,Php,Html,Ajax,Post,在理解如何访问服务器端的type=“file”输入时遇到一些困难。下面是我正在使用的代码。我使用AJAX是因为我希望我的web应用程序不需要刷新,并且我使用了一种稍微迂回的方式提交我的表单,这样我就可以有一个更好的UI 我的HTML: <form id="updateProfileImageForm" enctype="multipart/form-data"> <div class="updateProfileImageContainer">

在理解如何访问服务器端的type=“file”输入时遇到一些困难。下面是我正在使用的代码。我使用AJAX是因为我希望我的web应用程序不需要刷新,并且我使用了一种稍微迂回的方式提交我的表单,这样我就可以有一个更好的UI

我的HTML:

<form id="updateProfileImageForm" enctype="multipart/form-data">
            <div class="updateProfileImageContainer">
              <img src="images/profile/generic.png" class="updateProfileImage">
              <div class="updateProfileImageOverlay" onclick="changeImageToUpload()">
                <div class="updateProfileImageOverlayText">Upload new image</div>
              </div>
              <input type="file" id="imageToUpload" name="image" style="display: none;" onChange="$(this).closest('form').trigger('submit');"></input>
            </div>
          </form>
我的服务器PHP:

if (isset($_FILES['image'])) {
      $image = $_FILES['image'];
  }
$\u文件上的Var\u dump显示一个空数组,而$\u POST上的Var\u dump显示大量信息(我假设这是我的数据文件)。但是,访问$\u POST或$\u文件上的“image”属性(通过$\u POST['image']或$\u FILES['image'])会给我带来“未定义的索引”或“未定义的变量”

你们能不能教我:

  • $\u POST和$\u文件之间有什么区别

  • 在这种情况下,我应该如何访问该文件

  • 谢谢

    背后的想法。 不使用文件作为PHP的post,只需将图像转换为base64,并在PHP中使用ajax接收该字符串


    如果ajax请求中缺少必要的配置选项,则需要将contentType设置为false

      $.ajax({
        type: 'POST',
        url: '/changeProfile',
        data: form_data,
        processData: false,
        contentType: false,
        success: function(response) {
          if(response.message != null) {
            alert(response.message);
          } else {
            // Change profile image displayed
            $('.updateProfileImage').attr("src", response.newProfileImage);
            alert('Profile picture successfully updated! Refresh your browser to update your window!');
          }
        }
      })
    

    jQuery.ajax默认情况下将内容类型设置为application/x-www-form-urlencoded,这对于FormData对象是不正确的,如果将其设置为false,则基础XMLHttPrequest对象将正确设置内容类型。

    无论是
    $\u POST
    还是
    $\u文件
    都是存储不同信息的超全局变量。您只能使用
    $\u files
    访问文件信息。还有一个
    $\u请求
    ,它返回post和文件信息。好啊因此,让我们概括一下,
    $\u POST
    返回表单中的所有数据(不包括类型文件的输入),而
    $\u files
    则相反。以下内容不应是问题的原因,但排除了可能。从控件中删除style='display:none',然后查看它是否有效。如果是,则使用style='position:absolute;左:-999px;'这是一种处理文件上传的丑陋方式。如果文件类型不是图像,您会怎么做?虽然此链接可以回答问题,但最好在此处包含答案的基本部分,并提供链接以供参考。如果链接页面发生更改,则“仅链接”回答可能无效。由于您只上载图像,因此应使用验证来检测图像是否有效。在其他情况下,这将无济于事。
      $.ajax({
        type: 'POST',
        url: '/changeProfile',
        data: form_data,
        processData: false,
        contentType: false,
        success: function(response) {
          if(response.message != null) {
            alert(response.message);
          } else {
            // Change profile image displayed
            $('.updateProfileImage').attr("src", response.newProfileImage);
            alert('Profile picture successfully updated! Refresh your browser to update your window!');
          }
        }
      })