【问题标题】:Error while uploading file asynchronously异步上传文件时出错
【发布时间】:2016-06-22 15:29:18
【问题描述】:

我正在尝试使用 ajax 上传文件,如下所示

这是我的html代码:

<form id="upload-file" enctype="multipart/form-data" role="form"
   method="POST">
   <input type="file" name="image" id="file-data" />
   <button type="submit" class="btn btn-warning" style="margin-top:10px;">
   <a style="color:#fff;font-family:sans-serif;font-size:15px;">Submit</a>
   </button>
 </form>

jquery 代码:

   $("#upload-file").on('submit', function(e){             
     var formURL="fileupload.php";
     $('#message').html('Uploading File...');
     $("#message").css("display","");
     $.ajax(
     {
        url : formURL,
        type: "POST",
        data : new FormData(this),
        cache: false,
        processData:false,
        success:function(data, textStatus, jqXHR) 
        {   
          if (data) 
           {
            console.log(data);
            var obj = JSON.parse(data);
            $("#file-name span").html("");
            $("#file-size span").html("");
            $("#file-type span").html("");
            //$("#file-error").html("");
            $("#file-name span").html(obj['name']);
            $("#file-size span").html(obj['size']);
            $("#file-type span").html(obj['type']);
            //$("#file-error").html(obj['error']);
            $("#file-name").css("display","");
           } 
          },
        });
      });  

php代码:

  <?php
   require('db_connect.php');
   if(isset($_FILES['image'])){
  //$errors= array();
  $file['name'] = $_FILES['image']['name'];
  //echo $file['name'];
  $file_size =$_FILES['image']['size'];
  $file['size'] = $file_size/1000;
  $file_tmp =$_FILES['image']['tmp_name'];
  $file['type']=$_FILES['image']['type'];
  $file_ext=strtolower(end(explode('.',$_FILES['image']['name'])));

  $expensions= array("pdf");

  if(in_array($file_ext,$expensions)=== false){
     $file['error']="extension not allowed, please choose a pdf file.";
  }

  if($file_size > 2097152){
     $file['error']='File size must be excately 2 MB';
 }

  if(empty($errors)==true){
     move_uploaded_file($file_tmp,"fileoploaded/".$file_name);
  }
  else{
     $file['error'] = "File could'nt be updates";
  }
}
 $data = json_encode($file, true);
 //echo "done!";
 echo  $data;
 ?>

但我收到以下错误:

<b>Warning</b>:  Unknown: Input variables exceeded 1000. 
    To increase the limit change max_input_vars in php.ini. in 
   <b>Unknown</b> on line <b>0</b>

如果数据量大,我可以将 max_input_vars 更改为更高的值 我可以猜测之前的数据,但不能在用户上传文件的情况下, 有没有其他方法可以在不改变的情况下解决这个问题 php.ini 中的 max_input_vars?

【问题讨论】:

  • 请包含您的 PHP 代码。
  • 也许重启你的服务器以反映 php.ini 的变化?
  • 我不想更改 php.ini 文件。

标签: php html ajax


【解决方案1】:

您需要在$.ajax参数中添加contentType: false。 否则 jquery 会将其设置为默认 contentType(默认为application/x-www-form-urlencoded,在正文中包含二进制数据,它将文件拆分为十亿个单独的部分)。

【讨论】:

    猜你喜欢
    • 2013-01-12
    • 2023-03-29
    • 2018-02-01
    • 2012-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多