hurriedly% 2011-05-13 07:35 采纳率: 100%
浏览 38

如何在node.js中上传Ajax文件?

要使用ajax上载文件,请使用此上载器:http://valums.com/ajax-upload/

在node.js中,编写如下代码就可以处理正常的文件上传,而不需要Ajax:

console.log("request " + sys.inspect(req.headers));

            req.on('data', function(chunk) {
              console.log("Received body data:");
              // console.log(chunk.toString());
            });
            var form = new formidable.IncomingForm();

            form.parse(req, function(err,fields, files) {
                console.log('in if condition'+sys.inspect({fields: fields, files: files}));
                fs.writeFile("upload/"+files.upload.name, files.upload,'utf8', function (err) {
                    if (err) throw err;
                    console.log('It\'s saved!');
                    client.putFile("upload/"+files.upload.name, files.upload.name, function(err, res){
                        if (err) throw err;
                        if (200 == res.statusCode) {
                          console.log('saved to s3');

                          httpres.writeHead(200, {'content-type': 'text/plain'});
                          httpres.write('received 1upload:

');
                          httpres.end();
                        }
                    }); 
                });
            });

但为什么会提示出错?

request { host: 'myhost:8001',
  'user-agent': 'Mozilla/5.0 (Windows NT 5.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1',
  accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
  'accept-language': 'en-us,en;q=0.5',
  'accept-encoding': 'gzip, deflate',
  'accept-charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.7',
  'keep-alive': '115',
  connection: 'keep-alive',
  origin: 'http://myhost',
  'access-control-request-method': 'POST',
  'access-control-request-headers': 'content-type' }

events.js:45
        throw arguments[1]; // Unhandled 'error' event
                       ^
Error: bad content-type header, no content-type
    at IncomingForm._parseContentType (/usr/local/lib/node/.npm/formidable/1.0.0/package/lib/formidable/incoming_form.js:196:17)
    at IncomingForm.writeHeaders (/usr/local/lib/node/.npm/formidable/1.0.0/package/lib/formidable/incoming_form.js:109:8)
    at IncomingForm.parse (/usr/local/lib/node/.npm/formidable/1.0.0/package/lib/formidable/incoming_form.js:65:8)
    at Server.<anonymous> (/home/myfolder/myfolder/newcontentserver.js:29:18)
    at Server.emit (events.js:67:17)
    at HTTPParser.onIncoming (http.js:1108:12)
    at HTTPParser.onHeadersComplete (http.js:108:31)
    at Socket.ondata (http.js:1007:22)
    at Socket._onReadable (net.js:678:27)
    at IOWatcher.onReadable [as callback] (net.js:177:10)

我认为问题是没有通过ajax在标头中设置内容类型。如何在此上传器中设置标头?或者如何通过传入数据在node.js服务器中创建文件?因为它使用的是xhr,所以我认为不能使用formidable.incomingform()。

  • 写回答

1条回答 默认 最新

  • weixin_33743703 2011-05-13 08:06
    关注

    Looking at the latest fileuploader.js source code on github he does set the content type

    xhr.setRequestHeader("Content-Type", "application/octet-stream");

    I believe your problem is elsewhere.

    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?