如何修复:"stream ended unexpectedly" 当我尝试使用 adonis 框架在节点 js 中上传文件时

How to fix: "stream ended unexpectedly" when i tried to upload file in node js using adonis framework

我正在尝试将文件上传到本地文件夹,我使用了官方网站中的代码,只是照字面意义复制了它,但当我尝试上传时它总是显示 "stream ended unexpectedly"。

我在路由中的函数中尝试了console.log,但没有成功或没有响应。所以我认为问题是当我尝试将文件发送到路由器时,但我不知道到底是什么问题以及如何解决它。请帮我。对不起,如果我的英语不好。

<form method="POST" action="/upload" enctype="multipart/form-data">
 <input type="file" name="profile_pic">

 <button type="submit"> Submit </button>

</form>



const Helpers = use('Helpers')

Route.post('/upload', async ({ request }) => {
console.log("hai")
const profilePic = request.file('profile_pic', {
  types: ['image'],
  size: '2mb'
})

await profilePic.move(Helpers.tmpPath('uploads'), {
  name: 'custom-name.jpg'
})

if (!profilePic.moved()) {
  return profilePic.error()
}
return 'File moved'
})

我遇到了同样的问题。这可能与 "size" 控制变量。请尝试使用此代码...

// getting the image
const image = request.file('image', {
    maxSize: '20mb',
    allowedExtensions: ['jpg', 'png', 'jpeg']
    })

// move image to uploads folder
await image.move(Helpers.tmpPath('uploads'), {
    name: image_name,
    overwrite: false
})

if (!image.moved()) {
    return image.error()
}

已经修复了,因为我用的adonis框架不是通常的adonis(带前端),我用的adonis只适用于API。

这就是为什么,以通常的方式安装一个新的 adonis 项目(不只是 API)修复它。