ajaxForm 无法 POST 文件

ajaxForm unable to POST file

本文关键字:文件 POST 无法 ajaxForm      更新时间:2023-09-26

我想做什么

我有一张通过ajaxForm发布的表格。表单包含一个file输入字段,但是数据不会与POST中的其余信息一起处理。

代码

网页表单

<form id="profilepicForm" action="user/profilepic.php" method="post" enctype="multipart/form-data">
    <input type="file" accept="image/gif, image/jpeg, image/png" name="file" />
    <input type="hidden" name="userid" value="<?php echo $_SESSION['user']['id'] ?>" />
    <input type="submit" value="Upload">
</form>

爪哇语

var options = { 
    complete: function(response) {
        $("#profilepicMessage").html(response.responseText);
    },
    error: function(){
        $("#profilepicMessage").html("ERROR: unable to upload file");
    } 
};
$("#profilepicForm").ajaxForm(options);

.PHP

$user_id    = $_POST['userid'];
$image      = $_FILES['file']['name'];
print_r($_POST);
exit;

发生了什么事情

所有通过的都是Array ( [userid] => 34 ),其中34是我的特殊userid。因此,我知道表格正在发布,但文件没有通过。

您应该查看全局变量 $_FILES。

你必须寻找类似 uploadifive 的东西来管理它,因为 ajax 目前无法处理文件传输

(这并不完全正确,有 ajax2 和 HTML5 文件 API,但省去一个问题,寻找 UploadiFive)。