node.js - Nodejs通过http发送文件

标签 node.js file

我正在尝试将文件发送到另一个 node.js 服务。因此,我使用 http 和 form-data 模块。

这是我写的代码

function uintToString(uintArray) {
    return String.fromCharCode.apply(null, new Uint8Array(uintArray));
}


function (file) {     
    var data = uintToString(file.buffer);
//
var crlf = "\r\n",
    boundaryKey = Math.random().toString(16),
     boundary = `--${boundaryKey}`;
    delimeter = `${crlf}--${boundary}`,
    preamble = "", // ignored. a good place for non-standard mime info
    epilogue = "",
    headers = [
        'Content-Disposition: form-data; name="file"; filename="' + name + '"' + crlf
    ],
    closeDelimeter = `${delimeter}--`,
    multipartBody = Buffer.concat(
        new Buffer(preamble + delimeter + crlf + headers.join('') + crlf),
        data,
        new Buffer(closeDelimeter + epilogue)
    ); 


    var options = {
        host: 'localhost',
        port: 3000,
        method: 'POST',
        path: '/data/get',
        headers: {
            'Content-Type': 'multipart/form-data; boundary=' + boundary,
            'Content-Length': formData._valueLength
        }
    };

    //make request
    return httpsRequest(formData, options)
        .then((result) => {

           console.log(result);
        }, (err) => {

            console.log(err);

        });
};


function httpsRequest(data, options) {

    return new Promise(function (resolve, reject) {
        // request object
        var req = https.request(options, function (res) {
            var result = '';
            res.on('data', function (chunk) {
                result += chunk;
            });
            res.on('end', function () {
                console.log("https end result - " + result);
                resolve(result);
            });
            res.on('error', function (err) {
                reject(err);
            })
        });

        // req error
        req.on('error', function (err) {
            reject(err);
        });

        //send request witht the postData form
        req.write(data);
        req.end();

    });
}

它给出了““list”参数必须是缓冲区数组”此错误。 httpsRequest 函数似乎有问题。

最佳答案

不要重新发明轮子,needle/request可以为您做到这一点。如果你想 promise 一些事情,请使用 bluebird

const Promise = require('bluebird')
const needle = Promise.promisifyAll(require('needle'))

function send (file) {
  let url = 'https://localhost/data/get'
  let data = {
    zip_file: {
      buffer       : file.buffer,
      filename     : name,
      content_type : 'application/octet-stream'
    }
  }

  return needle.postAsync(url, data, { multipart: true })
}

关于node.js - Nodejs通过http发送文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43508267/

相关文章:

javascript - 在 express 之外的脚本中从 express 应用程序导入模块时出现问题

javascript - 为什么在实现全局作用域状态时我会在 react 中使用 'useReduce'?

javascript - 快速验证器 : show only one validation error message of a field

java - 从文件中的二进制内容中分离 ASCII 文本

java - JFileChooser 和 RMI

Java File .write() 整数流

node.js - 在 Node 依赖项中查找特定文件的位置的最佳方法?

linux - 如何在/mnt/中创建文件读/写权限?

ASP.NET 请求 - 我可以从客户端创建的动态控件获取发布的文件吗?

javascript - Node.js 根据其值返回对象键