Uploading a file with FormData and multer

后端 未结 2 1593
予麋鹿
予麋鹿 2021-02-10 19:13

I have successfully managed to upload a file to a Node server using the multer module by selecting the file using the input file dialog and then by submitting the f

2条回答
  •  南旧
    南旧 (楼主)
    2021-02-10 19:28

    to post a FormData object accepted by multer the upload function should be like this:

    function uploadFile(fileToUpload, url) { 
        var formData = new FormData();
        //append file here 
        formData.append('file', fileToUpload, fileToUpload.name);
        //and append the other fields as an object here
        /* var user = {name: 'name from the form',
            email: 'email from the form' 
            etc...       
        }*/
        formData.append('user', user);
    
        // This function simply creates an XMLHttpRequest object
        // Opens the connection and sends form_data
        doJSONRequest("POST", "/tracks/upload", null, formData, function(d) {
            console.log(d);
        })
    }
    

提交回复
热议问题