【问题标题】:how to upload image or any file using hapijs如何使用 hapijs 上传图像或任何文件
【发布时间】:2021-02-21 10:03:46
【问题描述】:

如何使用 Hapi.js 保存任何类型的文件或图像

{
    method: "POST",
    path: apiUrl + "/company",
    config: {
        description: "Create Company api",
        auth: false,
        payload: {
            maxBytes: 20715200,
            output: "stream",
            parse: true,
            allow: "multipart/form-data",
        },
        
    },
    handler:  (request, h) => {
        const attributes = request.payload;
        //attributes.companyName    <<== Comapny name (Text)
        //attributes.logo           <<== company logo (Image)
        try {
            //how to save company logo in specific path
        } catch (error) {
            
        }
    }
},

我对使用哪种类型的包或任何类型感到困惑?保存文件

【问题讨论】:

    标签: node.js hapijs hapi


    【解决方案1】:

    使用@hapi/hapi: 18.x.x + 版本将帮助您处理以下代码。

    server.route({
       method: "POST",
       path: apiUrl + "/company",
       handler:  (request, h) => {
          const attributes = request.payload;
          //attributes.companyName    <<== Comapny name (Text)
          //attributes.logo           <<== company logo (Image)
          try {
              //how to save company logo in specific path
          } catch (error) {
            
          }
       },
       options:{
          auth: false, // false by default
          payload: {
             parse: true,
             multipart: {
                    output: 'stream'
             },
             maxBytes: 1000 * 1000 * 5, // 5 Mb
          }
       }
    });
    

    编码愉快!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-06-21
      • 1970-01-01
      • 1970-01-01
      • 2018-09-11
      • 1970-01-01
      • 2013-01-16
      • 2020-07-14
      相关资源
      最近更新 更多