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 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 关于#python#的问题:自动化测试