简体   繁体   中英

Nodejs Formdata file upload

I am getting my image object from canvas and converting it into blob and then to file in java script

        var sendingData = new FormData();
        var ctx = mainCanvas.getContext("2d");
        console.log("canveas content =" + noofchildren.get(i));
        ctx.drawImage(noofchildren.get(i), 0, 0, mainCanvas.width, mainCanvas.height);

        var b64Data = mainCanvas.toDataURL("image/jpeg", 0.7).split(",")[1];
        var byteCharacters = atob(b64Data);
        var byteNumbers = new Array(byteCharacters.length);
        for (var j = 0; j < byteCharacters.length; j++) {
            byteNumbers[j] = byteCharacters.charCodeAt(j);
        }
        var byteArray = new Uint8Array(byteNumbers);
        var blob = new Blob([byteArray], { type: contentType });
        var file = new File([blob], "sample.JPG", { type: "image/jpeg" });

        sendingData.append(nameOfTheImageFiles[i], file);

        var ajaxRequest = $.ajax({
        type: "POST",
        url: "/api/" + email + "/imagesupload",
        contentType: 'multipart/form-data',
        processData: false,
        data: sendingData
    }).success(function (data) {
        console.log(data);
        $("#image-container").children().remove();
    });

i am using ajax to post my file to nodejs server

and req.files is an empty object in nodejs

How to resolve?

In asp.net I am getting the file on post method

Looking at the documentation of FormData.append() , you can see that you can only have a File as value when you have specified the third filename parameter. So try swapping the relevant line to the following

sendingData.append(nameOfTheImageFiles[i], file, "sample.JPG");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM