php 我可以将 jquery post 与图片上传一起使用吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9042164/
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-08-26 06:01:53  来源:igfitidea点击:

Can I use jquery post with image uploads?

phpjquery

提问by Paul

I'm trying to use jquery to submit my form like so but it's not triggering anything in the functions.php file. Do I need to do anything special with a multipart/form-data? Am I missing something?

我正在尝试使用 jquery 像这样提交我的表单,但它没有触发 functions.php 文件中的任何内容。我需要对多部分/表单数据做任何特别的事情吗?我错过了什么吗?

HTML:

HTML:

<form id="process_image_form" enctype="multipart/form-data" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF'], ENT_QUOTES); ?>" method="post" >
    <input type="hidden" name="image_id1" id="image_id1" />
    <table class="bordered-table">
        <tbody>                 
            <tr>
                <td><input name="file" type="file" id="file"/></td>
            </tr>
            <tr> 
                <td><button type="submit" class="btn primary" data-loading-text="Uploading..." id="upload_profile_photo" name="upload_profile_photo">Upload</button></td> 
            </tr>
        </tbody>
    </table>
</form>

Jquery call:

jQuery 调用:

$('#upload_profile_photo').click(function(e) {
   e.preventDefault();

    $.post('gallery/functions.php', $("#process_image_form").serialize(), function(status) {
        if (status.st) {
            alert("Photo Uploaded");
        }
    }, "json");
});

functions.php:

函数.php:

if (isset($_POST['upload_profile_photo'])) {

    if (isset($_FILES['file']) && $_FILES['file']['size'] > 0) {

        //handle file upload
        $size = filesize($_FILES['file']['tmp_name']);
        if ($size > $max_file_size * 1024 * 1024) {
            $res->error = '<div class="alert-message error">Your image file is too large. Reduce its size and try uploading again.</div>';
            echo json_encode($res);
            exit();
        }
        if ($res->error == "") {
            //process image
            $res = uploadImage($_FILES['file']['tmp_name'], $user_id);
            if ($res->st) {
                unlink($_FILES['file']['tmp_name']);
                $res->msg = '<div class="alert-message success">Your profile photo was uploaded successfully!</div>';
                echo json_encode($res);
                exit();
            }
        }
    }
    else {
        $res->error = '<div class="alert-message error">Please select a photo to upload.</div>';
        echo json_encode($res);
        exit();
    }
}

采纳答案by clime

jquery serialize method will not work on file inputs. Look here please: How can I upload files asynchronously?

jquery 序列化方法不适用于文件输入。请看这里:如何异步上传文件?



I have a great experience with jquery file uploadplugin for this purpose.

为此,我对jquery 文件上传插件有很好的经验。