admin管理员组

文章数量:1401849

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 I am getting the file on post method

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 I am getting the file on post method

Share Improve this question edited Dec 19, 2015 at 4:54 Renganatha Arunachalam asked Dec 18, 2015 at 13:41 Renganatha ArunachalamRenganatha Arunachalam 3491 gold badge3 silver badges15 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 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");

本文标签: javascriptNodejs Formdata file uploadStack Overflow