繁体   English   中英

上载图片时如何解决jQuery ajax post方法未定义索引问题?

[英]How to solve this jQuery ajax post method undefine index problem while uploading images?

我想使用Ajax Post方法上传照片,这是我的html和javascript代码

 <script> $(document).ready(function(){ $('#multiple_upload_form').submit(function(e){ e.preventDefault(); var upload = $('#images').val(); $.ajax({ type:'POST', url:'album.php', data: { upload : images }, cache:false, contentType:false, processData:false, success:function(data) { $('#image_preview').html(data); }, error:function() { $('#image_preview').html('error'); } }); return false; }); }); </script> 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form method="post" action="album.php" name="upload_form" id="multiple_upload_form" role="form" enctype="multipart/form-data"> <div class="form-group"> <label for="email">Album Name</label> <input type="text" name="aname" class="form-control" id="aname"> </div> <div class="form-group"> <label for="file">Upload Photos:</label> <input type="file" id="images" name="images" /> </div> <div id="images_preview"></div> </div> <center class="feedback" style="display:none">Loading...</center> <button id="submit" name="submitt" class="btn btn-default">Submit</button> </form> 

这是我的PHP编码

if(isset($_FILES['images']['name']) )
{
    $img = $_FILES['images']['name'];

    if(!empty($img))
    {
        echo 'MaxSteel';
    }
}
else
{
    echo 'Same Problem';
}

我有未定义索引:图像在输入type =“ text”时可以正常工作,但是当涉及到“ file”类型时,显示错误帮助我解决此问题

可能由于某些错误而导致文件未上传。 您应该以UPLOAD_MAX_SIZE等形式查看与文件上传相关的参数。

您可以使用var_dump($ file)查看$ _FILE内容。

您需要通过FormData()发送数据。 尝试这样的事情:

var images = $("#images").get(0).files;
var formData = new FormData();

$.each(images, function(i, image) {
  data.append("images[]", image);
});

然后将formData作为$ .ajax数据发送。

通过这段代码,可能会有所帮助

                   var data = new FormData();  
                    data.append("Avatar", file.files[0]);
                    $.ajax({
                     url: "http://192.168.1.1:2002/,
                     type: "POST",             
                     data:data,
                     contentType: false,      
                     cache: false, 
                     processData:false,  
                     success: function(data)
                      {
                     console.log(data);
                      }

                     });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM