阿贾克斯文件上传中的node.js [英] Ajax file upload in node.js

查看:174
本文介绍了阿贾克斯文件上传中的node.js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想用ajax上传文件中查找该用这个上传 http://valums.com/ajax-upload/

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

和node.js中写code这正与正常文件上传没有Ajax。

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)

我觉得问题是内容类型未在头阿贾克斯文件上传.thats设置为什么这个错误是来了我怎么能设置在此上传的头,或者我怎么能做出的node.js服务器上的文件由传入的数据。 由于它使用的是XHR,所以我想我不能使用 formidable.incomingform()我应该使用什么呢?

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?

推荐答案

纵观最新的 fileuploader.js 在github源$ C ​​$ C 他不设置内容类型

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

xhr.setRequestHeader(内容类型,应用程序/八位字节流);

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

I believe your problem is elsewhere.

这篇关于阿贾克斯文件上传中的node.js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆