将上传的文件传输到具有节点的远程服务器(最好使用相同的文件名) [英] Pipe a uploaded file to a remote server with node (ideally with same filename)

查看:193
本文介绍了将上传的文件传输到具有节点的远程服务器(最好使用相同的文件名)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的app.js服务器上传一个文件,这个文件应该把这个文件传到一个像我的upload.js服务器那样的跨域服务器上。
$ b

完整的代码可以在以下链接中找到



upload.js服务器正在工作。我的问题是app.js服务器。请求似乎可以流式传输文件( https://github.com/request/request#streaming)。但我没有得到它的工作。
我总是得到:[错误:无效的协议]在我的app.js。



它是这样一行:

  fs.createReadStream(file.path).pipe(request.post('localhost:4000 / upload'))

我把post改了,放在我的upload.js中,post方法也放了,但是导致同样的错误。 b
$ b我的目标是将一个文件从html页面上传到localhost:3000 / upload,它将文件传输到localhost:4000 / upload(理想情况下使用相同的文件名)。 >但我没有得到它的工作(这个后< a>并不帮助我)。
$ b

app.js:

  var express = require('express')
,multiparty = require('multiparty')
,request = require('request')
,fs = require('fs')
,util = require('util')
,http = requ IRE( HTTP);

var app = express();
app.use('/ static',express.static('static'));

process.on('uncaughtException',function(err){
console.log(err);
});
$ b $ app.get('/',function(req,res){
res.redirect('static / index.html');
});
$ b app.post('/ upload',function(req,res,next){

//https://github.com/request/request#streaming

var form = new multiparty.Form();
$ b $ form.parse(req,function(err,fields,files){
res.writeHead(200, {'content-type':'text / plain'});
res.write('received upload:\\\
\\\
');
res.end(util.inspect({fields :字段,文件:文件}));
));
form.on('file',function(name,file){

//将其流转到本地主机:4000同名
fs.createReadStream(file.path).pipe(request.post('localhost:4000 / upload'))

console.log(file.path);
});

});

var server = app.listen(3000,'0.0.0.0',function(){
var host = server.address()。address;
var port = server .address().port;

console.log('示例应用程序在http://%s:%s',主机,端口上侦听);
});

index.html:

 <!DOCTYPE html> 
< html>
< head lang =en>
< meta charset =UTF-8>
< title>上传< / title>
< / head>
< body>
< h2>上传< / h2>
< form method =postaction =/ uploadenctype =multipart / form-data>
< input type =fileid =filename =file/>
<按钮>上传< /按钮>
< / form>
< / body>
< / html>

upload.js:

  var express = require('express')
,multiparty = require('multiparty')
,cors = require('cors')
,util = require('util')
,app = express();

app.use(cors());
process.on('uncaughtException',function(err){
console.log(err);
});
$ b app.get('/',cors(),function(req,res,next){
res.json({msg:'这是启用CORS的所有来源! '});
});
$ b $ app.post('/ upload',cors(),function(req,res,next){
var form = new multiparty.Form();

form.parse(req,function(err,fields,files){
res.writeHead(200,{'content-type':'text / plain'});
res.write '收到上传:\\\
\\\
');
res.end(util.inspect({fields:fields,files:files}));
});
form。 on('file',function(name,file){
console.log(file);
console.log(name);
});

});
$ b app.listen(4000,function(){
console.log('CORS-enabled web server listening on port 4000');
});


更新



我相信你错过了url的协议。
如果你把 http 协议添加到url:

  code> fs.createReadStream(file.path).pipe(request.post('http:// localhost:4000 / upload'))

设置上传功能

当您在上传时将文件内容传送到POST功能。 js,多部分表单数据丢失了。您需要创建一个新的POST请求并传递原始文件内容。



app.js

$ $ p $ $ $ $ $ $ $ $ $ $ $ var formData = {
file:{
value:fs.createReadStream(file.path),
options:{
filename:file.originalFilename
}
}
};

//将文件发送到上传服务器
request.post({url:'http:// localhost:4000 / upload',formData:formData} );
}

这也会传递原始文件名
更多信息请参阅:
https://github.com/request/request#multipartform- data-multipart-form-uploads

I want to upload a file in my app.js server , which should pipe that file to a crossdomain server like my upload.js server.

The full code can be found under the following link

The upload.js server is working. My problem is the app.js server. Request seems to be capable of streaming files (https://github.com/request/request#streaming). But I dont get it working. I am always getting : [Error: Invalid protocol] in my app.js.

It musst be this line:

fs.createReadStream(file.path).pipe(request.post('localhost:4000/upload'))

I changed post to put and in my upload.js the post method also to put, but it results in the same error.

My Goal is to upload a file from the html page to localhost:3000/upload which pipes the file to localhost:4000/upload (ideally with same filename) . But I dont get it working (this post doesn't helped me).

app.js:

var express = require('express')
    , multiparty = require('multiparty')
    , request = require('request')
    , fs = require('fs')
    , util = require('util')
    , http = require('http');

var app = express();
app.use('/static', express.static('static'));

process.on('uncaughtException', function (err) {
    console.log(err);
});

app.get('/', function (req, res) {
    res.redirect('static/index.html');
});

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

    //https://github.com/request/request#streaming

    var form = new multiparty.Form();

    form.parse(req, function(err, fields, files) {
        res.writeHead(200, {'content-type': 'text/plain'});
        res.write('received upload:\n\n');
        res.end(util.inspect({fields: fields, files: files}));
    });
    form.on('file', function(name,file) {

        //stream it to localhost:4000 with same name
        fs.createReadStream(file.path).pipe(request.post('localhost:4000/upload'))

        console.log(file.path);
    });

});

var server = app.listen(3000, '0.0.0.0' ,function () {
    var host = server.address().address;
    var port = server.address().port;

    console.log('Example app listening at http://%s:%s', host, port);
});

index.html:

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>Upload</title>
</head>
<body>
  <h2>Upload</h2>
  <form method="post" action="/upload" enctype="multipart/form-data">
    <input type="file" id="file" name="file" />
    <button>upload</button>
  </form>
</body>
</html>

upload.js:

var express = require('express')
    , multiparty = require('multiparty')
    , cors = require('cors')
    , util = require('util')
    , app = express();

app.use(cors());
process.on('uncaughtException', function (err) {
    console.log(err);
});

app.get('/', cors(), function(req, res, next){
    res.json({msg: 'This is CORS-enabled for all origins!'});
});

app.post('/upload', cors(), function(req, res, next){
    var form = new multiparty.Form();

    form.parse(req, function(err, fields, files) {
        res.writeHead(200, {'content-type': 'text/plain'});
        res.write('received upload:\n\n');
        res.end(util.inspect({fields: fields, files: files}));
    });
    form.on('file', function(name,file) {
        console.log(file);
        console.log(name);
    });

});

app.listen(4000, function(){
    console.log('CORS-enabled web server listening on port 4000');
});

解决方案

Updated

I believe you're missing the protocol from the url. It should work, if you add the http protocol to the url:

fs.createReadStream(file.path).pipe(request.post('http://localhost:4000/upload'))

Make Upload work

When you pipe the file contents to the POST function in upload.js, the multipart form data is lost. You need to create a new POST request and pass the original file contents.

Do the following in app.js:

 form.on('file', function(name, file) {

    var formData = {
      file: {
        value:  fs.createReadStream(file.path),
        options: {
          filename: file.originalFilename
        }
      }
    };

    // Post the file to the upload server
    request.post({url: 'http://localhost:4000/upload', formData: formData});
}

This will also pass the original filename. For more information see: https://github.com/request/request#multipartform-data-multipart-form-uploads

这篇关于将上传的文件传输到具有节点的远程服务器(最好使用相同的文件名)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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