按下按钮后刷新页面,用ajax上传文件

问题描述 投票:1回答:1

我有此表单,我想通过ajax将文件和其他一些信息发送到PHP文件我的问题是,当我尝试下载文件时,按下发布后页面刷新,如何防止刷新页面?

[另外,我想清楚地了解这三个选项是什么,对于我每个人到底需要什么,我从这里得到了(另一篇文章的堆栈溢出)contentType: false,cache: false,processData: false,

当尝试不使用这三个选项进行上传时,我只是在控制台日志中得到了此错误消息(非法调用)。

<form action="" method="post" id="image-form" enctype="multipart/form-data">
    <input type="file" id="imgInp" name="imgInp"/>
    <input type="text" id="postNum" name="postNum" value="<?= $postNum; ?>"/>
    <input type="button" id="sendImage" value="Upload"/>
    <div id="blah"></div>
</form>
$("#sendImage").click(function() {
    var thisData = $('#image-form')[0];
    console.log(thisData);
    $("#msg").html('<div class="alert alert-info"><i class="fad fa-spin fa-spinner"></i> Please wait...!</div>');
    $.ajax({
        type: "POST",
        method: "POST",
        url: "data.php?uploadImage=1",
        data: new FormData(thisData),
        contentType: false,
        cache: false,
        processData: false,
        success: function(data) {
            console.log(data);
            if (data == 1 || parseInt(data) == 1) {
                $("#msg").html(
                    '<div class="alert alert-success"><i class="fa fa-thumbs-up"></i> Data updated successfully.</div>'
                );
            } else {
                $("#msg").html(
                    '<div class="alert alert-info"><i class="fa fa-exclamation-triangle"></i> Extension not good only try with <strong>GIF, JPG, PNG, JPEG</strong>.</div>'
                );
            }
        },
        error: function(data) {
            $("#msg").html(
                '<div class="alert alert-danger"><i class="fa fa-exclamation-triangle"></i> There is some thing wrong.</div>'
            );
        }
    });
});
$postNum= $_POST['postNum'];
$file = $_FILES['imgInp']['name'];
$file_image = '';
if($_FILES['imgInp']['name']!=""){
    extract($_REQUEST);
    $infoExt        =   getimagesize($_FILES['imgInp']['tmp_name']);
    if(strtolower($infoExt['mime']) == 'image/gif' || strtolower($infoExt['mime']) == 'image/jpeg' || strtolower($infoExt['mime']) == 'image/jpg' || strtolower($infoExt['mime']) == 'image/png'){
        $file   =   "Q-".$postNum.".".pathinfo($file, PATHINFO_EXTENSION);;
        $path   =   'includes/images/uploads/specialUploads/'.$file;
        move_uploaded_file($_FILES['imgInp']['tmp_name'],$path);

        $insert     =   1;
        if($insert){ echo 1; } else { echo 0; }
    }else{
        echo 2;
    }
}

谢谢

jquery ajax upload
1个回答
0
投票

使用

© www.soinside.com 2019 - 2024. All rights reserved.