dieeman
0.14D
V2EX  ›  Node.js

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

  •  
  •   dieeman · Aug 29, 2018 · 6083 views
    This topic created in 2820 days ago, the information mentioned may be changed or developed.

    有个需求需要在 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 replies    2018-08-30 09:18:55 +08:00
    7anshuai
        1
    7anshuai  
       Aug 29, 2018
    cuijie
        2
    cuijie  
       Aug 30, 2018
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2813 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 00:25 · PVG 08:25 · LAX 17:25 · JFK 20:25
    ♥ Do have faith in what you're doing.