Hapi.js文件上传如何获取文件,以便我可以使用imageMagick命令行工具

时间:2014-11-09 23:58:55

标签: javascript node.js pdf asyncfileupload hapijs

我更改了代码以输出文件而不是流。 IT为我提供了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

has is 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>

1 个答案:

答案 0 :(得分: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);
            }
       },
   });