1

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

1 Answer 1

3

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");
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.