hapi.js文件上传

前端部分:使用axios上传FormData格式的文件

     const formData = new FormData();
     formData.append("file", options.file);
     formData.append("name", options.file.name);
     upload(formData).then((res) => {});

后端部分:路由配置multipart.output设置为data

{
        path: '/upload',
        method: 'POST',
        options: {
            payload: {
                maxBytes: 1024 * 1024 * 100,
                parse: true,
                output: 'data',
                allow: 'multipart/form-data',
                multipart: {
                    output: 'data'
                }
            }
        },
        handler: (request, h) => upload(request, h)
  }

upload.ts

import { Request, ResponseToolkit } from '@hapi/hapi'
import fs from 'fs'
import path from 'path'
export const upload = (request:Request, h:ResponseToolkit) => {
    const { file, name } = request.payload as any
    fs.writeFile(path.resolve() + `/static/imags/${name}`, file, (err) => {
        if (err)console.log(err, 'rr')
    })
    return 1
}

hapi静态内容:
参考链接

静态内容展示

你可能感兴趣的:(hapi.js文件上传)