如何获得xhtmlrequest响应?

时间:2015-07-04 01:00:41

标签: javascript ajax

如何从PHP文件中获取响应? client.responseText无效

我不知道在这里输入什么,但它说它需要更多细节? 所以我尝试编写一个文件上传代码,这是一个很小的开始,但对于错误处理我需要知道响应。

function fileChange() {

    var fileList = document.getElementById("fileA").files;

    //File Objekt (erstes Element der FileList)
    var file = fileList[0];

    //File Objekt nicht vorhanden = keine Datei ausgewählt oder vom Browser nicht unterstützt
    if (!file)
        return;

    document.getElementById("fileName").innerHTML = 'Dateiname: ' + file.name;
    document.getElementById("fileSize").innerHTML = 'Dateigröße: ' + file.size + ' B';
    document.getElementById("fileType").innerHTML = 'Dateitype: ' + file.type;
    document.getElementById("progress").value = 0;
    document.getElementById("prozent").innerHTML = "0%";
}


var client = null;

function uploadFile() {
    //Wieder unser File Objekt
    var file = document.getElementById("fileA").files[0];
    var interpret = document.getElementById("interpret").value;
    var title = document.getElementById("title").value;
    alert(interpret);
    alert(title);
    //FormData Objekt erzeugen
    var formData = new FormData();
    //XMLHttpRequest Objekt erzeugen
    client = new XMLHttpRequest();

    var prog = document.getElementById("progress");

    if (!file)
        return;

    prog.value = 0;
    prog.max = 100;


    formData.append("datei", file);
    formData.append("interpret", interpret);
    formData.append("title", title);

    client.onerror = function(e) {
        alert("onError");
    };

    client.onload = function(e) {
        document.getElementById("prozent").innerHTML = "100%";
        prog.value = prog.max;
    };

    client.upload.onprogress = function(e) {
        var p = Math.round(100 / e.total * e.loaded);
        document.getElementById("progress").value = p;
        document.getElementById("prozent").innerHTML = p + "%";
    };

    client.onabort = function(e) {
        alert("Upload abgebrochen");
    };

    client.open("POST", "./ajax/fileupload.php");
    client.send(formData);
}

function uploadAbort() {
    if (client instanceof XMLHttpRequest)
        client.abort();
}

0 个答案:

没有答案