我如何处理文件上传?

时间:2016-05-12 17:05:18

标签: javascript jquery node.js http file-upload

我如何处理通过curl上传的文件。我有快递js动作/路线

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

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

我尝试使用此命令上传文件 $ curl -v -F filedata=@image.jpg 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)的访问权限并执行我想要的操作?提前谢谢。

1 个答案:

答案 0 :(得分:0)

经过一番研究,我遇到了connect-bus-boy。这就是我实现上传路径的方式。首先安装busboy 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' });

        });
    });


    });
相关问题