将图像文件数据从前端发送到后端

有没有一种方法可以将图像文件从前端发送到后端? 我需要将表单中选定的图像上传到云计算云平台。

req.body访问文件input没有帮助,并给我一个错误,说no such file or directory open

如果我添加enctype="multipart/form-data" ,然后在我的后端做一个req.on('data', (data)=> {console.log(data.toString)}) ,然后执行上传它cloudinary,它失败,并给我以下错误: Error: Path must be a string without null bytes

这是我的forms:

 <form method="post" action="/submitArticle" enctype="multipart/form-data"> <div class="file"> <label class="file-label"> <input class="file-input" type="file" name="image"> <span class="file-cta"> <span class="file-icon"> <i class="fa fa-upload"></i> </span> <span class="file-label"> Choose a file… </span> </span> </label> </div> <div class="field"> <label class="title is-4">Title</label> <div class="control"> <input class="input" name="title" type="text" placeholder="Give your story a title..."> </div> </div> <div class="field"> <label class="title is-4">Story</label> <div class="control"> <textarea class="textarea" name="story" rows="7" placeholder="Tell your story here..."></textarea> </div> </div> <div class="field is-grouped is-grouped-right"> <p class="control"> <input class="button is-primary" type="submit"> </input> </p> </div> </form> 

有没有另外一种方法将图像数据从前端发送到后端? 提前致谢!!

如果你想上传到Cloudinary,也许你应该考虑使用Cloudinary Upload Widget 。

该小部件支持直接从浏览器上传图像到Cloudinary存储,而不涉及您的服务器在过程中。

你可以使用像Dropzone这样的第三方库来在前端上传图片并发送到后端。 简单就是这样