2016-05-12 80 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' }); 

     }); 
    }); 


    });