[英]How to fix: “stream ended unexpectedly” when i tried to upload file in node js using adonis framework
I'm trying to upload file to a local folder, and I used the code in the official website and i just copied it like literally but it always show "stream ended unexpectedly" when I tried to upload. 我正在尝试将文件上传到本地文件夹,我在官方网站上使用了代码,我只是按字面意思复制它,但是当我尝试上传时,它总是显示“流意外结束”。
I tried console.log in the function in the route, but it didn't work or didn't responds. 我在路由中的函数中尝试了console.log,但它没有工作或没有响应。 So i think the problem is when I try to send the file to the router, but i don't know what exactly the problem and how to fix it.
所以我认为问题是当我尝试将文件发送到路由器时,但我不知道究竟是什么问题以及如何解决它。 Please help me.
请帮我。 Sorry if my English is bad.
对不起,如果我的英语不好。
<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'
})
I had the same problem. 我有同样的问题。 It likely has to do with the "size" control variable.
它可能与“大小”控制变量有关。 Try this code instead...
请尝试使用此代码...
// 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()
}
already fixed, it's because of the adonis framework that I had is not the usual adonis (with frontend), the adonis that i'm using is for API only. 已经修复了,因为我所拥有的adonis框架不是通常的adonis(有了前端),我使用的adonis仅用于API。
that's why, installing a new adonis project the usual way (not API only) fixed it. 这就是为什么,以通常的方式安装新的adonis项目(不仅仅是API)修复它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.