将上传的文件流式传输到HTTP请求 [英] Streaming an uploaded file to an HTTP request

查看:2453
本文介绍了将上传的文件流式传输到HTTP请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是接受上传的文件,并使用 Wistia /wistia.com/doc/upload-api rel = nofollow> Wistia上传API 。我需要能够向HTTP请求中添加字段,并且我不希望文件与磁盘接触。我正在使用节点 Express 请求 Busboy

My goal is to accept an uploaded file and stream it to Wistia using the the Wistia Upload API. I need to be able to add fields to the HTTP request, and I don't want the file to touch the disk. I'm using Node, Express, Request, and Busboy.

下面的代码有两个 console.log 语句。第一个返回 [错误:未实现] ,第二个返回 [错误:表单数据:未实现] 。我是Node流媒体的新手,所以我可能做的是根本错误的事情。任何帮助将不胜感激。

The code below has two console.log statements. The first returns [Error: not implemented] and the second returns [Error: form-data: not implemented]. I'm new to streaming in Node, so I'm probably doing something fundamentally wrong. Any help would be much appreciated.

app.use("/upload", function(req, res, next) {
    var writeStream = new stream.Writable();
    writeStream.on("error", function(error) {
        console.log(error);
    });
    var busboy = new Busboy({headers: req.headers});
    busboy.on("file", function(fieldname, file, filename, encoding, mimetype) {
        file.on("data", function(data) {
            writeStream.write(data);
        });
        file.on("end", function() {
            request.post({
                url: "https://upload.wistia.com",
                formData: {
                    api_password: "abc123",
                    file: new stream.Readable(writeStream)
                }
            }, function(error, response, body) {
                console.log(error);
            });
        });
    });
    req.pipe(busboy);
});


推荐答案

我不熟悉busboy模块,但是尝试使用未实现的流会导致错误。每当您直接从 stream 模块创建新的可读或可写流时,都必须创建 _read _write 方法分别流实现器(node.js api)。为了使您能够使用以下示例,使用 multer 处理多部分请求,我认为您会发现multer比busboy更易于使用。

I am not to familiar with the busboy module, but there errors you are getting are from attempting to use un-implemented streams. Whenever you create a new readable or writable stream directly from the stream module you have to create the _read and _write methods respectively Stream Implementors (node.js api). To give you something to work with the following example is using multer for handling multipart requests, I think you'll find multer is easier to use than busboy.

var app = require('express')();
var fs = require('fs');
var request = require('request');
app.use(multer());

app.post("/upload", function(req, res, next) {
    // create a read stream
    var readable = fs.createReadStream(req.files.myfile.path);
    request.post({
        url: "https://upload.wistia.com",
        formData: {
            api_password: "abc123",
            file: readable
        }    
    }, function(err, res, body) {
        // send something to client
    })
});

不幸的是,我希望这会有所帮助,但我不熟悉busboy,但是应该与multer一起使用,并且我说过,在出现问题之前,只是您正在使用未实现的流,如果可以的话,我确定可以使用busboy配置此操作。

I hope this helps unfortunately I am not familiar with busboy, but this should work with multer, and as I said before there problem is just that you are using un-implemented streams I'm sure there is a way to configure this operation with busboy if you wanted.

这篇关于将上传的文件流式传输到HTTP请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆