简体   繁体   English

如何解决:当我尝试使用adonis框架在节点js上传文件时“流意外结束”

[英]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.

相关问题 当我想使用 adonis js 在 Windows 操作系统中上传图像时,我发现了这个问题,我该如何解决这个问题 - When I want to upload image in windows os with adonis js I found this problem how can I solved this 如何使用节点js上传文件? - How do I upload a file using node js? 尝试再次上传时如何使用ng-file-upload保留以前上传的文件? - How to retain visibly previous uploaded files using ng-file-upload when tried to upload again? 当我尝试运行节点 js 文件时,出现 ReferenceError: XMLHttpRequest is not defined - When I tried to run a node js file .I am getting ReferenceError: XMLHttpRequest is not defined 如何使用 Node.js 将 JSON stream 写入文件? - How can I write a JSON stream to file using Node.js? 如何使用 busboy 检测上传流意外停止 - How to detect an upload stream stopped unexpectedly with busboy 我想使用node.js上传文件 - I Want to Upload a File Using node.js 我使用节点 js multer 遇到文件上传问题 - I'm getting file upload problem using node js multer NODE.JS:如何确保读取流已结束并且数据已写入? - NODE.JS : How to make sure a reading stream has ended and the data written? 如何在Node.js中将文件附件流式传输到响应? - How do I stream a file attachment to a response in node.js?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM