下载解压缩上传clodinary

时间:2016-02-13 19:50:30

标签: node.js unzip cloudinary

我正在尝试下载一个ZIP文件,请求将其传递给node-unzip,然后将其传输到cloudinary。

var stream = cloudinary.uploader.upload_stream(function(result) {
  console.log(result)
});

request(file_url).pipe(unzip.Parse()).pipe(stream); //<-- This do not work!
ERROR: { error: { message: 'Empty file', http_code: 400 } }

fs.createReadStream('my_picture.jpg').pipe(stream); //<-- This works!

1 个答案:

答案 0 :(得分:1)

(您没有提及,但我假设您的request var是request的实例)

首先检查您的zip是否有效并包含您期望的内容:

(代码取自node-unzip's readme

var unzip = require('unzip');
var request = require('request');
var fstream = require('fstream');
var writeStream = fstream.Writer('output/path');

request(file_url)
  .pipe(unzip.Parse())
  .pipe(writeStream);

然后,从zip中发送一个条目(即使只有一个):

var cloudinary = require('cloudinary');
var unzip = require('unzip');
var request = require('request');
var stream = cloudinary.uploader.upload_stream(function(result) {
  console.log(result)
});
request(file_url)
  .pipe(unzip.Parse())
  .on('entry', function (entry) {
    var fileName = entry.path;
    var type = entry.type; // 'Directory' or 'File'
    var size = entry.size;
    if (fileName === "your_image.png") {
      entry.pipe(stream);
    } else {
      entry.autodrain();
    }
  });