Nodejs Formdata文件上传

时间:2015-12-18 13:41:22

标签: javascript node.js

我从画布获取我的图像对象并将其转换为blob,然后转换为java脚本文件

        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();
    });

我正在使用ajax将我的文件发布到nodejs服务器

和req.files是nodejs中的空对象

如何解决?

在asp.net中,我在post方法

上获取文件

1 个答案:

答案 0 :(得分:0)

查看FormData.append()的文档,您可以看到在指定第三个File参数时,您只能使用filename作为值。因此,请尝试将相关行交换到以下

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