javascript PHP文件上传,发送formdata对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19690044/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 16:29:52  来源:igfitidea点击:

PHP file upload, sending formdata object

javascriptphpmysqlajax

提问by Erik

I want to upload a file, send it to javascript formData object and than via ajax to some php script which will put the file into database. It works fine when i upload a file in profile php, send it to formData object in javascript and send it back to profile.php. Problem is that when i want to send formData object to some other php script which isn't profile.php, it doesn't work.

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

Here is my code:

这是我的代码:

profile.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

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);
                }
            })
        }
    });

And than I want to check in some other php (script.php) script if file was uploaded.

如果文件已上传,我想检查其他一些 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");
    }
}

回答by Erik

Added to jQuery.ajax and it works now:

添加到 jQuery.ajax 并且它现在可以工作:

processData: false,

contentType: false,