Pipe a uploaded file to a remote server with node (ideally with same filename)

前端 未结 3 1408
时光说笑
时光说笑 2020-12-15 07:36

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 foll

3条回答
  •  别那么骄傲
    2020-12-15 08:35

    I had a similar problem but I wanted to stream the file directly to the remote server instead of saving it locally first. Here's my modifications to get streaming to work:

    app.post('/upload', function(request, response, next) {
        var form = new multiparty.Form();
    
        form.on('part', function(formPart) {
            var contentType = formPart.headers['content-type'];
    
            var formData = {
                file: {
                    value: formPart,
                    options: {
                        filename: formPart.filename,
                        contentType: contentType,
                        knownLength: formPart.byteCount
                    }
                }
            };
    
            requestJs.post({
                url: 'http://localhost:4000/upload',
                formData: formData,
    
                // These may or may not be necessary for your server:
                preambleCRLF: true,
                postambleCRLF: true
            });
        });
    
        form.on('error', function(error) {
            next(error);
        });
    
        form.on('close', function() {
           response.send('received upload');
        });
    
        form.parse(request);
    });
    

提交回复
热议问题