【问题标题】:Ajax file upload in node.js在 node.js 中上传 Ajax 文件
【发布时间】:2011-08-24 17:28:36
【问题描述】:

想要为此使用此上传器使用 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:\n\n');
                          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() 我应该使用什么?

【问题讨论】:

  • 我们需要查看您上传表单的 javascript。

标签: ajax file-upload node.js


【解决方案1】:

查看最新的fileuploader.js source code on github他确实设置了内容类型

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

我相信您的问题出在其他地方。

【讨论】:

  • @Rayons 但我认为我不能使用强大的,我需要使用其他东西来上传
猜你喜欢
  • 2018-06-02
  • 2011-04-13
  • 1970-01-01
  • 2014-11-30
  • 1970-01-01
  • 2022-10-07
  • 2014-04-19
  • 2016-02-14
相关资源
最近更新 更多