0

我更改了代码以输出文件而不是流。它为我提供了 tmp 路径,当我使用 fs.readFile 转换为字符串时的数据是

fileUpload=Resume_BrianInoa.pdf

我正在向 hapijs 服务器发布一个文件,这是我处理该帖子的路线:

  server.route({
   method: 'POST',
   path: '/convert',
   config: {
        payload: {
           output:'file',
           maxBytes:209715200,
            parse: false,
            allow: 'application/x-www-form-urlencoded'
        },
        handler:function (request, reply) {

           console.log('path : ' + request.payload.path);
         //   request.payload["fileUpload"].pipe(fs.createWriteStream("test"));
             fs.readFile(request.payload.path, function (err, data) {
                if(err)
                   console.error(err);
                else
                   console.log(data.toString());
               // I want to rewrite the file to a new folder here 
               // Then convert it using imageMagick's command line tool
               //  var newPath = __dirname + "/uploads/" + "newFile.txt" ;
               //  fs.writeFile(newPath, data, function (err) {
               //    console.log(err);
               //    reply('done');
               //  });

             });
          }
   },

这是我的 request.payload

path : /tmp/1415580285921-24240-2cc7987f4fd124ac

我实际上检查了我的 /tmp/ 文件夹并打开了它唯一的文件

/tmp/1415580285921-24240-2cc7987f4fd124ac

有是 fileUpload=Resume_BrianInoa.pdf 文件没有正确上传

我的表单的 html 代码

<form action="./convert" method="post">
<input type="file" name="fileUpload" id="fileUpload" enctype="multipart/form-data" class="form-control">
<button class="btn">Submit</button>
</form>
4

1 回答 1

3

总结一下你的html应该是这样的 -

<form action="./convert" method="POST" enctype="multipart/form-data">
    <input type="file" name="fileUpload" id="fileUpload" class="form-control"> 
    <button class="btn">Submit</button>
</form>

和你的hapi路线

   server.route({
       method: 'POST',
       path: '/convert',
       config: {
            payload: {
               output: 'file',
               maxBytes: 209715200,
               //allow: 'multipart/form-data',
               parse: true //or just remove this line since true is the default
            },
            handler:function (request, reply) {   
               console.log('fileUpload path : ' + request.payload.fileUpload.path);
            }
       },
   });
于 2014-11-10T02:56:58.067 回答