hapi.js文件上传
2022-04-04 本文已影响0人
姜酱i
前端部分:使用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静态内容:
参考链接
