【发布时间】:2018-07-18 13:31:02
【问题描述】:
我是 node.js 的新手,我试图通过拖放来上传文件,
我首先创建了一个简单的上传器(无需拖放)
像这样,它成功了:
var http = require('http');
var formidable = require('formidable');
http.createServer(function (req, res) {
if (req.url == '/fileupload') {
var form = new formidable.IncomingForm();
form.parse(req, function (err, fields, files) {
res.write('File uploaded');
res.end();
});
} else {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write('<form action="fileupload" method="post" enctype="multipart/form-data">');
res.write('<input type="file" name="filetoupload"><br>');
res.write('<input type="submit">');
res.write('</form>');
return res.end();
}
}).listen(8080);
当我转到 http://localhost:8080/ 时,我可以看到上传者并且一切正常。
现在我正在尝试使用 dropzone 拖放来上传,而不是简单的上传,但我不知道如何让它工作
var http = require('http');
var formidable = require('formidable');
http.createServer(function (req, res) {
if (req.url == '/fileupload') {
var form = new formidable.IncomingForm();
form.parse(req, function (err, fields, files) {
res.write('File uploaded');
res.end();
});
} else {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write('<html><head><title>Dropzone </title><link href="dropzone.css" rel="stylesheet"></head><body><form method="post" action="/fileupload" id="uploader" enctype="multipart/form-data"><input name="file" type="file" multiple /><button>Save</button></form><script src="dropzone.js"></script></body></html>');
return res.end();
}
}).listen(8080);
现在,当我运行 $ node my-file.js 并转到 localhost:8080/ 时,我看到的是普通上传器,而不是 dropzone 上传器。
如果有人能帮我解决这个问题,我将不胜感激......
提前致谢
【问题讨论】:
-
您在表单中将
action属性保留为/ -
@Seblor thx,我修复了这个错误,但它仍然无法正常工作
标签: javascript node.js dropzone.js formidable