文件上传时的WebKitFormBoundary标头

时间:2014-12-03 16:43:33

标签: ajax node.js api file-upload

为了避免共享第三个服务api令牌,我想通过我的节点服务器上传我的文件。

要做到这一点,我的网页将我在ajax中的文件发送到我的节点服务器,该服务器将它们发送到第三方API。

所以:

client > ajax > node > api

我的文件成功到达第三方服务但使用WebKitFormBoundary页眉和页脚。

------WebKitFormBoundaryBvPXh46XrNOYDFJn
Content-Disposition: form-data; name="file"; filename="Screen Shot 2014-11-20 at 15.01.45.png"
Content-Type: image/png

<< file bytes >>
------WebKitFormBoundaryBvPXh46XrNOYDFJn--

Ajax代码:

$.uploadFiles = function(file){

        var data = new FormData();

        data.append('file', file, file.name);

        console.log(data);

        $.ajax({
            //url: 'https://slack.com/api/files.upload?token=xoxp-2964595734-2964595740-3128685143-b1796a&channels='+settings.joinedChannel+'&pretty=1',
            url: '/app/fileUpload?token='+settings.token+'&channel='+settings.joinedChannel,
            type: 'POST',
            data: data,
            cache: false,
            dataType: 'json',
            processData: false, // Don't process the files
            contentType: false, // Set content type to false as jQuery will tell the server its a query string request
            success: function(data, textStatus, jqXHR)
            {
                if(typeof data.error === 'undefined')
                {
                    // Success so call function to process the form
                    //submitForm(event, data);
                    console.log(data);
                }
                else
                {
                    // Handle errors here
                    console.log('ERRORS: ' + data.error);
                }
            },
            error: function(jqXHR, textStatus, errorThrown)
            {
                // Handle errors here
                console.log('ERRORS: ' + textStatus);
                // STOP LOADING SPINNER
            }
        });

    };

节点代码

app.post('/app/fileUpload', isAuthorized, function(req, res, next) {

    var token            = req.param('token');
    var channel          = req.param('channel');
    var req              = req;

    models.app.find({ where: { token: token, active: true } }).success(function(application) {
        slack.uploadFile(application.slack_api_token,channel,req);
        return res.status(200).json({ success: true, message: "Uploading" });
    });

});

[....]

uploadFile: function uploadFile(applicationToken,channel,file) {

    var formData = {
        // Pass a simple key-value pair
        token: applicationToken,

        // Pass a simple key-value pair
        channels: channel,

        // Pass data via Streams
        file: file

    };

    request.post('https://slack.com/api/files.upload?pretty=1', { formData: formData }, function (error, response, body) {
        if (!error && response.statusCode == 200 && typeof body.ok !== "undefined" && body.ok == true) {
            console.log("OK");
            //console.log(body);
        } else {
            console.log("Not OK");
            //console.log(response);
        }
    });

},

FIXED:使用formidable和此代码来管理文件流:

file: {
    value:  fs.createReadStream(stream.file.path),
        options: {
            filename: stream.file.name,
            contentType: stream.file.type
        }
    }

0 个答案:

没有答案