簡體   English   中英

通過ajax jquery上傳文件

[英]File uploading via ajax jquery

我正在嘗試使用jquery ajax上傳文件輸入,但不刷新頁面。 我的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>

而我的劇本是

$(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);
                }
            }
        });


    });

});

我的PHP頁面是

<?php

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

?>

但是我收到輸出警報,提示您離線!\\ n請檢查您的網絡。 如何解決這個問題。請任何人幫助。

工作正常。 下面是修改后的代碼。

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>

腳本

$(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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM