问题描述
所以,相对简单的问题。 我正在尝试创建一个简单的文件上传器。现在,我只想使用 JS/NodeJS 将文件从客户端传递到服务器。我已经阅读了许多论坛(其中大部分基本上是从 stackoverflow 中的线程摘录)遵循示例并尝试使我的代码尽可能简单,同时进行故障排除。这是我目前所拥有的:
选择文件的表格
<form action='/upload' method='post' enctype="multipart/form-data">
<input type="file" name="file" id="file" />
<input type='submit' value="upload" />
</form>
app.js
const express = require('express');
const Busboy = require('busboy');
const path = require('path');
const fs = require('fs');
const app = express();
app.post('/upload',Busboy(),(req,res) => {
var busboy = new Busboy({ headers: req.headers });
busboy.on('file',function(fieldname,file,filename,encoding,mimetype) {
console.log("On file.");
file.resume();
});
busboy.on('error',(err) => {
console.log("Error encountered");
})
busboy.on('finish',function() {
console.log("On finish");
res.writeHead(200,{ 'Connection': 'close' });
res.end("Finished!");
})
return req.pipe(busboy);
});
我在这里查看了一些类似的问题,但似乎没有一个解决方案适用。
[https://stackoverflow.com/questions/31186192/req-busboy-onfile-not-firing] 我没有缺少名称属性(afaik)
[https://stackoverflow.com/questions/28318002/connect-busboy-onfile-event-not-firing?] OP 没有提到修复它的内容,但我将 enctype 设置为 multipart/form-data这里给出的答案。
[https://stackoverflow.com/questions/42986926/express-busboy-on-file-event-not-firing?] 我已经读了很多遍了,我这辈子都看不下去了,看看他们在“更正”版本中的变化。
我不确定需要/相关的其他信息,但我在本地运行它并尝试上传小于 1MB 的 jpg 和 png。感谢任何帮助,谢谢!
编辑:我忘了提及当前的行为。它跳过了 on:file 触发器。 on:finish 正在按预期运行。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)