0

大家好,我正在尝试使用 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?
        }
    }
});
4

2 回答 2

1

更新并解决

let server=http.createServer((req, res)=>{
    if(req.url==='/upload'){
        let headers = {}
              // IE8 does not allow domains to be specified, just the *
            headers={
                'Access-Control-Allow-Origin':'*',
                'Access-Control-Allow-Methods':'POST, GET, PUT, DELETE, OPTIONS',
                'Access-Control-Allow-Credentials':false,
                'Access-Control-Max-Age':'86400', // 24 hours
                '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'){
            let form=new formidable.IncomingForm(),
                files=[],
                fields=[]
            form.uploadDir='/home/sonick7/'

            form.on('field', (field, value)=>{
                fields.push([field, value])
            })
            .on('file', (field, file)=>{
                files.push([field, file])
            })
            .on('end', ()=>{
                console.log(files, fields, 'FT here awefwa', __filename)
                console.log('Upload terminado ')
                res.writeHead(200, headers)
                res.end()
            })
            form.parse(req)
        }
    }
})
于 2020-09-20T02:51:14.743 回答
0

如果您收到来自客户端的请求,OPTIONS则可能意味着客户端首先发送 CORS 请求。我不确定您使用的是哪个客户端库,AFAKI AngularJS 将在每​​个 Ajax 请求中发送 CROS 请求。

所以你需要在服务器端处理这个OPTIONS请求,告诉客户端这是一个有效的 CROS 请求,然后客户端应该发送文件上传请求(POST也许)。

下面的代码是我在项目中使用模块在DEV环境中处理 CORS 请求的代码express,这意味着它允许来自任何来源的请求。在生产环境中,您需要检查原始标头并接受您信任的标头。

    应用程序配置(功能(){
        app.use(function (req, res, next) {
            var oneof = 假;
            如果(req.headers.origin){
                res.header('Access-Control-Allow-Origin', '*');
                一个=真;
            }
            if (req.headers['access-control-request-method']) {
                res.header('Access-Control-Allow-Methods', 'OPTIONS, HEAD, GET, POST, PUT, DELETE');
                一个=真;
            }
            if (req.headers['access-control-request-headers']) {
                res.header('Access-Control-Allow-Headers', 'Content-Type, Content-Range, Content-Disposition, Authentication');
                一个=真;
            }
            如果(其中一个){
                res.header('Pragma', '无缓存');
                res.header('Cache-Control', '无存储,无缓存,必须重新验证');
                res.header('Content-Disposition', 'inline; filename="files.json"');
            }
            if (其中一个 && req.method == 'OPTIONS') {
                res.send(200);
            }
            别的 {
                下一个();
            }
        });
    });
于 2014-03-20T04:58:16.547 回答