Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/453.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何通过HTML表单将文件上载到node js服务器?_Javascript_Node.js_Xmlhttprequest_File Handling - Fatal编程技术网

Javascript 如何通过HTML表单将文件上载到node js服务器?

Javascript 如何通过HTML表单将文件上载到node js服务器?,javascript,node.js,xmlhttprequest,file-handling,Javascript,Node.js,Xmlhttprequest,File Handling,我在通过HTML将文件上载到节点服务器时遇到问题。在页面中,我有以下输入: <input type="file" id = "csvFile" /> <input type="submit" name="submit" onclick="send_data()" /> } 这里有第一个问题,因为ready状态的arrow函数从不执行 无论如何,这是我第一次这样做,所以我不

我在通过HTML将文件上载到节点服务器时遇到问题。在页面中,我有以下输入:

<input type="file" id = "csvFile" />
<input type="submit" name="submit" onclick="send_data()" />
}

这里有第一个问题,因为ready状态的arrow函数从不执行


无论如何,这是我第一次这样做,所以我不知道如何确保我的服务器获得文件并处理它。有人能帮我吗?

这应该可以做到:

let file = document.getElementById("csvFile").files[0];
let xhr = new XMLHttpRequest();
let formData = new FormData();

formData.append("csvFile", file);
xhr.open("POST", '${your_full_address}');
xhr.onreadystatechange = function () {
    // In local files, status is 0 upon success in Mozilla Firefox
    if (xhr.readyState === XMLHttpRequest.DONE) {
        var status = xhr.status;
        if (status === 0 || (status >= 200 && status < 400)) {
            // The request has been completed successfully
            console.log(xhr.responseText);
        } else {
            // Oh no! There has been an error with the request!
        }
    }
};
xhr.send(formData);
let file=document.getElementById(“csvFile”).files[0];
设xhr=newXMLHttpRequest();
设formData=new formData();
append(“csvFile”,文件);
xhr.open(“POST”、“${your_full_address}”);
xhr.onreadystatechange=函数(){
//在本地文件中,在Mozilla Firefox中成功后状态为0
if(xhr.readyState==XMLHttpRequest.DONE){
var status=xhr.status;
如果(状态===0 | |(状态>=200&&status<400)){
//请求已成功完成
console.log(xhr.responseText);
}否则{
//哦,不!请求有错误!
}
}
};
xhr.send(formData);
参考:

let file = document.getElementById("csvFile").files[0];
let xhr = new XMLHttpRequest();
let formData = new FormData();

formData.append("csvFile", file);
xhr.open("POST", '${your_full_address}');
xhr.onreadystatechange = function () {
    // In local files, status is 0 upon success in Mozilla Firefox
    if (xhr.readyState === XMLHttpRequest.DONE) {
        var status = xhr.status;
        if (status === 0 || (status >= 200 && status < 400)) {
            // The request has been completed successfully
            console.log(xhr.responseText);
        } else {
            // Oh no! There has been an error with the request!
        }
    }
};
xhr.send(formData);