Hapi.js文件上传我如何获取文件,这样我就可以在上面使用imageMagick命令行工具

Hapi.js file upload how do I grab the file so I could use imageMagick command line tool on it

本文关键字:文件 在上面 就可以 命令行 imageMagick 工具 获取 js 何获取 Hapi      更新时间:2023-09-26

我更改了代码以输出文件而不是流。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');
               //  });
             });
          }
   },

这是我的请求。上传

path : /tmp/1415580285921-24240-2cc7987f4fd124ac

事实上,我检查了我的/tmp/文件夹并打开了它——文件

/tmp/141558025921-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>

总结一下,你的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);
            }
       },
   });