Upload multiple files with XMLHttpRequest to Express.js 3.5 Server

前端 未结 1 1768
终归单人心
终归单人心 2020-12-31 18:18

I\'m trying to build a file uploader with the native FileAPI in JavaScript and I want to upload the files via XMLHttpRequest (without jQuery) to a Node.js server, which uses

相关标签:
1条回答
  • 2020-12-31 19:04

    Thx to @Pengtuzi I solved it:

    I used the FormData API to upload the files. My mistake was that I thought the error would happen on the server.

    Here's the code that solved it for me:

    function uploadFiles(files) {
        var xhr = new XMLHttpRequest();
        var formData = new FormData();
        xhr.onload = successfullyUploaded;
        xhr.open("POST", "http://localhost:3000/upload", true);
        xhr.setRequestHeader('X-Requested-With','XMLHttpRequest');
        for(var file in files) {
            formData.append("uploads", files[file].data);
        }
        xhr.send(formData);
    }
    
    0 讨论(0)
提交回复
热议问题