4

我想上传一个文件,将其发送到 javascript formData 对象,然后通过 ajax 发送到一些 php 脚本,该脚本会将文件放入数据库。当我在配置文件 php 中上传文件,将其发送到 javascript 中的 formData 对象并将其发送回 profile.php 时,它工作正常。问题是,当我想将 formData 对象发送到其他不是 profile.php 的 php 脚本时,它不起作用。

这是我的代码:

配置文件.php

<form role="form" id="editUserProfile" method="post" action="" enctype="multipart/form-data">
    <input type="file" id="filename" name="filename" class="file-input" accept="image/*"/></a>
    <button type="submit">Save</button
</form>

javascript.js

    $('#editUserProfile').validate({
        submitHandler: function (form) {
            var aFormData = new FormData();

            aFormData.append("filename", $('#filename').get(0).files[0]);

            $.ajax({
                type: "POST",
                url: "script.php",
                data: aFormData,
                success: function(data){
                    window.location.reload(true);
                }
            })
        }
    });

如果文件已上传,我想检查其他一些 php (script.php) 脚本。

if(is_uploaded_file($_FILES['filename']['tmp_name']) && getimagesize($_FILES['filename']['tmp_name']) != false){
    $size = getimagesize($_FILES['filename']['tmp_name']);
    $type = $size['mime'];
    $imgfp = fopen($_FILES['filename']['tmp_name'], 'rb');
    $size = $size[3];
    $name = $_FILES['filename']['name'];
    $maxsize = 99999999;

    if($_FILES['userfile']['size'] < $maxsize ){
        $dbh = new PDO("mysql:host=localhost;dbname=test", 'root');
        $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

        $stmt = $dbh->prepare("INSERT INTO table (image_type ,image, image_size, image_name) VALUES (? ,?, ?, ?)");

        $stmt->bindParam(1, $type);
        $stmt->bindParam(2, $imgfp, PDO::PARAM_LOB);
        $stmt->bindParam(3, $size);
        $stmt->bindParam(4, $name);

        $stmt->execute();
    }else{
        throw new Exception("File Size Error");
    }
}
4

1 回答 1

6

添加到 jQuery.ajax 并且现在可以使用:

processData: false,

contentType: false,
于 2013-10-31T18:07:40.953 回答