大家好,我正在尝试使用 ajax 和节点发送文件,但我没有找到告诉我如何做的信息!!,我现在构建了一个脚本,但我可以关注更多
var server=http.createServer(function(req, res){
if(req.url==='/upload'){
if(req.method==='OPTIONS'){//OPTIONS is the method that show in the server when i send files
req.on('data', function(a){
console.log(a);//i dont know what are?? after to here!!
});
}
}
});
我听过一些模块,比如强大的、多部分的和busboy ,但我无法运行它
谢谢
我可以用 FORMIDABLE 解决它 :) 看下一个代码:
var server=http.createServer(function(req, res){
if(req.url==='/upload'){
var headers = {};
// IE8 does not allow domains to be specified, just the *
// headers["Access-Control-Allow-Origin"] = req.headers.origin;
headers["Access-Control-Allow-Origin"] = "*";
headers["Access-Control-Allow-Methods"] = "POST, GET, PUT, DELETE, OPTIONS";
headers["Access-Control-Allow-Credentials"] = false;
headers["Access-Control-Max-Age"] = '86400'; // 24 hours
headers["Access-Control-Allow-Headers"] = "X-Requested-With, X-HTTP-Method-Override, Content-Type, Accept";
if(req.method==='OPTIONS'){
res.writeHead(200, headers);
res.end();
}else if(req.method==='POST'){
var form = new formidable.IncomingForm(),
files = [],
fields = [];
form.uploadDir = '/home/sonick7/';//direccion donde va a ser gusradado
form.on('field', function(field, value) {
fields.push([field, value]);
})
.on('file', function(field, file) {
console.log(file.name, file.size, file.type, file.path)
files.push([field, file]);
})
.on('end', function() {
console.log('Upload terminado ');
res.writeHead(200, headers);
res.end();
});
form.parse(req);//no se que hace eso y para que sirve el modulo util?
}
}
});