V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
dieeman
V2EX  ›  Node.js

nodejs 发送 multipart/form-data 类型,有什么好的解决方案?

  •  
  •   dieeman · 2018-08-29 11:55:39 +08:00 · 5074 次点击
    这是一个创建于 2064 天前的主题,其中的信息可能已经有所发展或是发生改变。

    有个需求需要在 nodejs 后端发送 multipart/form-data 请求做文件上传。

    有个初步想法就是: 自己拼接表单到 body

    大概这样:

    request.write( '--' + boundaryKey + '\r\n'

    • 'Content-Type: application/octet-stream\r\n'

    • 'Content-Disposition: form-data; name="my_file"; filename="my_file.bin"\r\n'

    • 'Content-Transfer-Encoding: binary\r\n\r\n' ); fs.createReadStream('./my_file.bin', { bufferSize: 4 * 1024 }) .on('end', function() {

      request.end('\r\n--' + boundaryKey + '--'); }) // set "end" to false in the options so .end() isn't called on the request .pipe(request, { end: false })

    2 条回复    2018-08-30 09:18:55 +08:00
    7anshuai
        1
    7anshuai  
       2018-08-29 12:23:24 +08:00
    cuijie
        2
    cuijie  
       2018-08-30 09:18:55 +08:00
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3399 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 11:45 · PVG 19:45 · LAX 04:45 · JFK 07:45
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.