使用HapiJS上传多个文件

时间:2015-02-15 13:35:13

标签: node.js hapijs

我正在尝试将单个请求中的多个文件上传到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时,我只看到两个字段,"文件"和"提交名称"文件字段包含--BbC04y边界之间的所有内容,即" - BbC04y \ r \ nConContent-Disposition:file; filename =" file1.txt" \ r \ nConContent-Type:text / plain \ r \ n ...-- BbC04y - "

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

1 个答案:

答案 0 :(得分:0)

取自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

获得它
相关问题