Node.js中的Ajax文件上传

发布于 2024-11-07 03:45:13 字数 2910 浏览 0 评论 0原文

想要使用 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() 我应该使用什么?

Want to upload a file using ajax for this using this uploader
http://valums.com/ajax-upload/

and in node.js write this code which is working with normal file upload without 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();
                        }
                    }); 
                });
            });

But this is giving error ?

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)

I think problem is the content-type is not set in the headers By ajax file upload .thats why this error is coming how can i set the header in this uploader , or how can i make a file in node.js server by incoming data.
As it is using the xhr so i think i can't use the formidable.incomingform() what should i use?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼泪也成诗 2024-11-14 03:45:13

查看最新的 fileuploader.js github 上的源代码 他确实设置了内容类型

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

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文