簡體   English   中英

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

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

我更改了代碼以輸出文件而不是流。 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>

只是總結你的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);
            }
       },
   });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM