简体   繁体   English

通过ajax jquery上传文件

[英]File uploading via ajax jquery

I am trying to upload file input with jquery ajax but without refreshing the page. 我正在尝试使用jquery ajax上传文件输入,但不刷新页面。 my html form is 我的HTML表格是

 <form name="uploadform" id="uploadform" method="post" enctype="multipart/form-data">
 <div name="profileBiodata" id="profileBiodata" style="display:none">
            <input type="file" id="file" name="file">
            <input type="submit"  id="submit" value="Upload" >
        </div>
</form>

and my script is 而我的剧本是

$(document).ready(function() {

    // process the form
    $('#uploadform').submit(function() {
        var filename = $("#file").val();

        $.ajax({
            url: '../PhpProject1/ProfileBiodataUpload.php',
            dataType: 'json',
            type: 'POST',
            data: {"op": "upload",  "filename": filename},
            success: function(response) {

                if (response.status == "success") {
                    alert("file exists");

                }
                else if (response.status == "failure")

                {
                    alert("file not exists");

                }


            },
            error: function(x, e) {
                if (x.status === 0) {
                    alert('You are offline!!\n Please Check Your Network.');
                } else if (x.status === 404) {
                    alert('Requested URL not found.');
                } else if (x.status === 500) {
                    alert('Internel Server Error - Please try after relogin to the application');
                } else if (e === 'parsererror') {
                    alert('Parsing JSON Request failed.');
                } else if (e === 'timeout') {
                    alert('Request Time out.');
                } else {
                    alert('Unknow Error.\n' + x.responseText);
                }
            }
        });


    });

});

my php page is 我的PHP页面是

<?php

if (isset($_POST['filename']))
{
        echo '{"status" : "success"}';
     }

?>

but i am getting the output alert You are offline!!\\n Please Check Your Network. 但是我收到输出警报,提示您离线!\\ n请检查您的网络。 how to solve this.Any one help please.thanks in advance 如何解决这个问题。请任何人帮助。

It works fine. 工作正常。 Below is the modified code. 下面是修改后的代码。

HTML form HTML表格

<form name="uploadform" id="uploadform" method="post" enctype="multipart/form-data">
    <div name="profileBiodata" id="profileBiodata">
        <input type="file" id="file" name="file">
        <input type="submit"  id="submit" value="Upload" >
    </div>
</form>

Script 脚本

$(document).ready(function() {

// process the form
$('#uploadform').submit(function() {
    var filename = $("#file").val();

    $.ajax({
        url: 'ProfileBiodataUpload.php',
        dataType: 'json',
        type: 'POST',
        data: {"op": "upload",  "filename": filename},
        success: function(response) {
            if (response.status == "success") {
                alert("file exists");
            }
            else if (response.status == "failure")
            {
                alert("file not exists");
            }
        },
        error: function(x, e) {
            if (x.status === 0) {
                alert('You are offline!!\n Please Check Your Network.');
            } else if (x.status === 404) {
                alert('Requested URL not found.');
            } else if (x.status === 500) {
                alert('Internel Server Error - Please try after relogin to the application');
            } else if (e === 'parsererror') {
                alert('Parsing JSON Request failed.');
            } else if (e === 'timeout') {
                alert('Request Time out.');
            } else {
                alert('Unknow Error.\n' + x.responseText);
            }
        }
    });
    return false;
});
});

ProfileBiodataUpload.php ProfileBiodataUpload.php

<?php
if (isset($_POST['filename']))
{
    echo '{"status" : "success"}';
}
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM