【问题标题】:Uploading multiple files with HapiJS使用 HapiJS 上传多个文件
【发布时间】:2015-02-15 22:24:56
【问题描述】:

我试图在一个请求中将多个文件上传到 Hapi JS 服务器。到目前为止,我还没有成功。这是原始请求(取自 w3org,以使过程尽可能简单)。

Content-Type: multipart/form-data; boundary=AaB03x

--AaB03x
Content-Disposition: form-data; name="submit-name"

Larry
--AaB03x
Content-Disposition: form-data; name="files"
Content-Type: multipart/mixed; boundary=BbC04y

--BbC04y
Content-Disposition: file; filename="file1.txt"
Content-Type: text/plain

... contents of file1.txt ...
--BbC04y
Content-Disposition: file; filename="file2.gif"
Content-Type: image/gif
Content-Transfer-Encoding: binary

...contents of file2.gif...
--BbC04y--
--AaB03x--

这是 Hapi 端的处理程序配置:

path: '/1.1/playbacks/new',
method: 'POST',
config: {
    payload: {
        maxBytes: 209715200,
        output: 'file',
        parse: true
    },
    auth: 'token'
}

当我调试 request.payload 时,我只看到两个字段,“files”和“submit-name”,其中 files 字段包含 --BbC04y 边界之间的所有内容,即“--BbC04y\r \nContent-Disposition: file; filename="file1.txt"\r\nContent-Type: text/plain\r\n...--BbC04y--"

那么将多个文件上传到 Hapi JS 的正确方法是什么?

【问题讨论】:

    标签: node.js hapijs


    【解决方案1】:

    取自 w3org 的样本是错误的!请求应该是这样的:

    ------WebKitFormBoundaryfXvbZd3ZABBHzmdC
    Content-Disposition: form-data; name="name"
    
    123456
    ------WebKitFormBoundaryfXvbZd3ZABBHzmdC
    Content-Disposition: form-data; name="last"
    
    789000
    ------WebKitFormBoundaryfXvbZd3ZABBHzmdC
    Content-Disposition: form-data; name="upload1"; filename="test1"
    Content-Type: application/octet-stream
    
    file 1
    
    ------WebKitFormBoundaryfXvbZd3ZABBHzmdC
    Content-Disposition: form-data; name="upload2"; filename="test2"
    Content-Type: application/octet-stream
    
    file 2
    
    ------WebKitFormBoundaryfXvbZd3ZABBHzmdC--
    

    请注意,不需要不同的边界值。它们都是一样的。

    借助奇妙的工具http://requestb.in得到它

    【讨论】:

      猜你喜欢
      • 2021-02-21
      • 1970-01-01
      • 2014-05-04
      • 2016-08-10
      • 1970-01-01
      • 2011-09-23
      • 1970-01-01
      • 2017-01-13
      • 2012-10-10
      相关资源
      最近更新 更多