【问题标题】:I Can't Uploading File with fs.createWriteStream and req.pipe我无法使用 fs.createWriteStream 和 req.pipe 上传文件
【发布时间】:2021-06-25 23:45:13
【问题描述】:

我阅读了这个教程并尝试了一下:
https://debugmode.net/2014/01/14/how-to-upload-file-in-node-js/

html:

<form action="/post-a-file" method="post" enctype="multipart/form-data" >
<input type="file" name="file" required>
</form>

app.js:

app.post("/post-a-file",  function (req, res){

    var destinationFile = fs.createWriteStream(__dirname + "/movies");
    req.pipe(destinationFile)
    
   req.on('end', ()=>{ console.log("end") })
})

/movies中没有文件。

【问题讨论】:

    标签: html node.js forms file-upload node-modules


    【解决方案1】:

    你不能这样做。您的帖子以multipart/form-data 的身份到达,您需要中间件来为您解析。然后,该中间件可能会为您提供将生成的文件数据放入文件的选项。有关该中间件的一种选择,请参阅multer。如果您只是上传单个文件,您可能希望使用 multer 的 upload.single() 变体。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-03-09
    • 1970-01-01
    • 1970-01-01
    • 2021-11-09
    • 1970-01-01
    • 2015-11-09
    • 2019-06-30
    相关资源
    最近更新 更多