我正在尝试将.mp3文件上传到本地的节点服务器。为此,我遵循了2种方法:
http.createServer(function(req, res){
if(req.url == "/fileupload"){
var form = new formidable.IncomingForm();
form.parse(req, function(err, fields, files){
var oldpath = files.filetoupload.path;
var newpath = './audio/audio.mp3';
fs.readFile(oldpath, function(err, data){
if(err) throw err;
fs.writeFile(newpath, data, function(err){
if(err) throw err;
res.write('<center>File uplaoded and saved</center>');
res.end();
});
fs.unlink(oldpath, function(err){
if(err) throw err;
});
});
});
}
else{
res.writeHead(200, {'Content-Type': 'text/html'});
res.write('<div><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></div>');
return res.end();
}
}).listen(4200);
它将在4200端口启动该应用程序,并且可用。
var express = require('express');
var app = express();
var server = require('http').Server(app);
var fs = require('fs');
var io = require('socket.io')(server);
var ss = require('socket.io-stream');
app.use(express.static(`${__dirname}/html`));
server.listen('5001');
app.get('/uploadform', function (req, res) {
if(req.url == "/fileupload"){
var form = new formidable.IncomingForm();
form.parse(req, function(err, fields, files){
var oldpath = files.filetoupload.path;
var newpath = './audio/audio.mp3';
fs.readFile(oldpath, function(err, data){
if(err) throw err;
fs.writeFile(newpath, data, function(err){
if(err) throw err;
res.writeHead(200, {'Content-Type': 'text/html'});
res.write('<center>File uplaoded and saved</center>');
res.end();
});
fs.unlink(oldpath, function(err){
if(err) throw err;
});
});
});
}
else{
res.writeHead(200, {'Content-Type': 'text/html'});
res.write('<div'>
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></div>');
return res.end();
}
});
在这里,当我选择文件并单击“提交”按钮时,它带我进入状态为404的错误页面,如下所示:
尝试了我已知的可能方法,但仍然需要有关如何解决此问题和顺利上传文件的帮助。
谢谢。
答案 0 :(得分:0)
基本上,您在代码中所说的是“当有对/ uploadform的get请求时,然后调用该函数”,然后在该函数中,您说req.url是否为“ / fileupload”工作。 req.url如何同时等于“ / uploadform”和“ / fileupload”。我认为最好的方法是
var express = require('express');
var app = express();
var server = require('http').Server(app);
var fs = require('fs');
var io = require('socket.io')(server);
var ss = require('socket.io-stream');
app.use(express.static(`${__dirname}/html`));
app.get('/uploadform', function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write('<div'>
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></div>');
return res.end();
});
app.post("/fileupload",function(req,res,next){
var form = new formidable.IncomingForm();
form.parse(req, function(err, fields, files){
var oldpath = files.filetoupload.path;
var newpath = './audio/audio.mp3';
fs.readFile(oldpath, function(err, data){
if(err) throw err;
fs.writeFile(newpath, data, function(err){
if(err) throw err;
res.writeHead(200, {'Content-Type': 'text/html'});
res.write('<center>File uplaoded and saved</center>');
res.end();
});
fs.unlink(oldpath, function(err){
if(err) throw err;
});
});
});
});
server.listen(process.env.port||'5001');
我希望对您有帮助