2016-05-12 87 views
0

如何处理通过curl.I上传的文件有这样的快递JS动作/路由我如何处理文件上传?

router.route('/images')  
    .post (function(req, res) { 
     res.status(200); 
     res.json({ message: 'file uploaded' }); 
    }); 

app.use('/api', router); 

,我尝试上传的文件使用此命令 $ curl -v -F [email protected] https://webapp.com/api/images

和我回来

* About to connect() to webapp.herokuapp.com port 443 (#0) 
* Trying 54.83.197.202... connected 
* successfully set certificate verify locations: 
* CAfile: none 
    CApath: /etc/ssl/certs 
* SSLv3, TLS handshake, Client hello (1): 
* SSLv3, TLS handshake, Server hello (2): 
* SSLv3, TLS handshake, CERT (11): 
* SSLv3, TLS handshake, Server key exchange (12): 
* SSLv3, TLS handshake, Server finished (14): 
* SSLv3, TLS handshake, Client key exchange (16): 
* SSLv3, TLS change cipher, Client hello (1): 
* SSLv3, TLS handshake, Finished (20): 
* SSLv3, TLS change cipher, Client hello (1): 
* SSLv3, TLS handshake, Finished (20): 
* SSL connection using ECDHE-RSA-AES128-GCM-SHA256 
* Server certificate: 
* subject: C=US; ST=California; L=San Francisco; O=Heroku, Inc.; CN=*.herokuapp.com 
* start date: 2014-01-21 00:00:00 GMT 
* expire date: 2017-05-19 12:00:00 GMT 
* subjectAltName: webapp.herokuapp.com matched 
* issuer: C=US; O=DigiCert Inc; OU=www.digicert.com; CN=DigiCert SHA2 High Assurance Server CA 
* SSL certificate verify ok. 
> POST /api/images HTTP/1.1 
> User-Agent: curl/7.22.0 (x86_64-pc-linux-gnu) libcurl/7.22.0 OpenSSL/1.0.1 zlib/1.2.3.4 libidn/1.23 librtmp/2.3 
> Host: webapp.herokuapp.com 
> Accept: */* 
> Content-Length: 6507 
> Expect: 100-continue 
> Content-Type: multipart/form-data; boundary=----------------------------5c1ec9129e4f 
> 
< HTTP/1.1 100 Continue 
< HTTP/1.1 200 OK 
< Server: Cowboy 
< Connection: close 
< X-Powered-By: Express 
< Content-Type: application/json; charset=utf-8 
< Content-Length: 27 
< Etag: W/"1b-u4Int4i4Ps6LpUaeuMtSFw" 
< Date: Thu, 12 May 2016 08:02:58 GMT 
< Via: 1.1 vegur 
< 
* Closing connection #0 
* SSLv3, TLS alert, Client hello (1): 
{"message":"File uploaded!"} 

而且在服务器日志我看到

` at=info method=POST path="/api/images" host=webapp.herokuapp.com request_id=e6c18dc4-4a3f-4fe7-ab70-198a83c985c8 fwd="99.89.114.63" dyno=web.1 connect=1ms service=189ms status=200 bytes=229` 

` 问题

我如何获得进入到实际的文件(FILEDATA)做什么,我想用它做什么?先谢谢你。

回答

0

经过一番研究,我碰到连接总线的boy.This是我如何实现我所有的上传route.First在代码中实现此安装打杂npm install busboy则...

var busboy = require('connect-busboy'); 
    app.use(busboy()); 
//... 

router.route('/images')  
    .post (function(req, res) { 

    var fstream; 
    req.pipe(req.busboy); 
    req.busboy.on('file', function (fieldname, file, filename) { 
     console.log("Uploading: " + (fieldname)); 
     fstream = fs.createWriteStream(__dirname + '/public/img/'+ filename); 
     file.pipe(fstream); 
     fstream.on('close', function() { 
     res.status(200); 
     res.json({ message: 'File uploaded' }); 

     }); 
    }); 


    });