Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use jquery post with image uploads?

Tags:

jquery

php

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?

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:

$('#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:

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();
    }
}
like image 275
Paul Avatar asked Jan 28 '12 01:01

Paul


People also ask

Can we upload image using ajax?

You can use jquery. form. js plugin to upload image via ajax to the server.

How do you preview an image before it is uploaded using jQuery?

First, we have a division element that contains the “img” tag. Using Jquery, we will change the “src” attribute of the “img” tag on upload to preview the image. The second element is the “input” tag. It is essential to specify type = “file” in this context.

How do I display an image before uploading HTML?

The most efficient way would be to use URL. createObjectURL() on the File from your <input>. Pass this URL to img. src to tell the browser to load the provided image.


1 Answers

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


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

like image 149
clime Avatar answered Sep 21 '22 23:09

clime