简体   繁体   English

通过ajaxform上传文件不起作用

[英]File upload through ajaxform not working

I am trying to upload an image through an AJAX call (& PHP). 我正在尝试通过AJAX调用(&PHP)上传图像。 I am using ajaxform plugin to do so but, apparently when I submit the form the "file" input value is not being passed to the php script. 我正在使用ajaxform插件来执行此操作,但是,显然,当我提交表单时,“文件”输入值未传递到php脚本。 Here is my HTML & JS: 这是我的HTML和JS:

<form action="upload_file.php" method="post" enctype="multipart/form-data" id="select-image">
    <input type="file" name="image" size="30"/> <input type="submit" name="upload" value="Upload" />
</form>

<script>
    var options = { 
        success:       showResponse  // post-submit callback 
    }; 

    // bind form using 'ajaxForm' 
    $('#select-image').ajaxForm(options); 

    // post-submit callback 
    function showResponse(responseText, statusText, xhr, $form)  {       
        alert('status: ' + statusText + '\n\nresponseText: \n' + responseText + 
            '\n\nThe output div should have already been updated with the responseText.'); 
    } 
</script>

And for testing upload.php contains: 为了测试upload.php包含:

<?php
if(isset($_POST['upload'])){
    var_dump($_FILES);
}
?>

When, I submit the form all I get back is: 什么时候提交表格,我回来的就是:

Array(
    [image]=>
)

Am I doing something wrong? 难道我做错了什么? Can I even upload image through Ajaxform plugin? 我什至可以通过Ajaxform插件上传图像吗? Suggestions please. 请提出建议。

Thanks. 谢谢。

Try to add 'cache : false' in var options. 尝试在var选项中添加'cache:false'。

var options = { 
  cache : false,
  success: showResponse  // post-submit callback 
}; 

Works for me. 为我工作。

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

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