通过模态窗口上传时保存重复图像

时间:2013-09-27 09:08:47

标签: javascript jquery ajax modal-dialog

我在模态窗口上做了一个图像上传器。工作正常。模态打开并上传图像,保存一个图像,工作正常!! 然后在关闭模态并再次打开它并尝试以模态显示图像时,问题开始,因为它将重复图像保存到2-4个随机时间。以为它是因为保存的事件所以尝试了preventDefault但是没有工作..或者有什么方法我可以完全销毁这个ajax调用然后可能工作。你有什么意见来解决这个问题?

的Ajax

<script type='text/javascript'>
$(document).ready(function() { 
    $('#form').live('change', function(event){
        event.preventDefault();
        $.ajax({
            type: 'POST',
            url: 'uploadajax.php',
            beforeSend: function(){
                $('#throw').html("working..");
            },
            success: function(data){
                $('#form').ajaxForm({                  
                    target: '#throw',
                }).submit();
            }
        });
        e.preventDefault();
        return false;
    });
});

服务器端

<?php
$path = "upload/";

if ($_FILES["image"]["type"] == "") {
    echo 'no file';
} else {

    if ((($_FILES["image"]["type"] == "image/gif")
            || ($_FILES["image"]["type"] == "image/jpeg")
            || ($_FILES["image"]["type"] == "image/jpg")
            || ($_FILES["image"]["type"] == "image/pjpeg")
            || ($_FILES["image"]["type"] == "image/x-png")
            || ($_FILES["image"]["type"] == "image/png"))) {

        if ($_FILES["image"]["type"] == "image/gif") {
            $ext = 'gif';
        }
        if ($_FILES["image"]["type"] == "image/jpeg") {
            $ext = 'jpeg';
        }
        if ($_FILES["image"]["type"] == "image/jpg") {
            $ext = 'jpg';
        }
        if ($_FILES["image"]["type"] == "image/pjpeg") {
            $ext = 'jpeg';
        }
        if ($_FILES["image"]["type"] == "image/x-png") {
            $ext = 'png';
        }
        if ($_FILES["image"]["type"] == "image/png") {
            $ext = 'png';
        }

        $img_url1 = rand(111111, 999999) . time() . substr(str_replace(" ", "_", 9999), 1);

        $img_url1 = $img_url1;

        $tmp = $_FILES['image']['tmp_name'];
        if (move_uploaded_file($tmp, $path . $img_url . "." . $ext)) {



            $link = $path . $img_url;

            $link1 = "$link.$ext";

            unset($tmp);
        }
    } else {
        echo 'Invalid file error';
    }
}
?>

模态窗口

<script type='text/javascript'>
$(document).ready(function() { 
    $('.uploadme').click(function(event){
        event.preventDefault();
        $.ajax({
            type: 'POST',
            url: 'uploadbox.php',
            beforeSend: function(data){
                box.dialog({
                    message: "Setting up upload box",
                    animate: false,
                    title: "<center>Upload Box</center>"
                });
            },
            success: function(data){
                box.hideAll()
                box.dialog({
                    animate: false,
                    message: data,
                    title: "<center>Upload Box</center>"
                });
            }
        });
        return false;
        e.preventDefault();
    });
});

0 个答案:

没有答案