将zip文件上传到服务以供用户下载

时间:2015-06-04 00:31:50

标签: node.js file-upload express amazon-s3 zip

我有一个express(node.js)应用程序,它为用户创建mp3文件并将它们存储在服务器上的文件夹中。

服务器的文件结构如下所示:

data/
  id#/
    song1.mp3
    song2.mp3
    song3.mp3
  id#/
    song1.mp3
    song2.mp3
    song3.mp3
  ...

我想创建zip文件并下载文件夹和电子邮件用户的zip文件链接。每个文件夹都使用id#命名,并对应于用户。

我想使用外部服务(例如s3)来存储和处理文件的下载。

如何压缩文件并将其发送到服务并创建下载链接?我应该研究哪些服务?

1 个答案:

答案 0 :(得分:1)

你可以使用child_process模块在​​后台运行一个zip命令(这个例子就是说,如果你在Linux上,你可以修改它以适应windows)

这只是处理zip过程,然后您可以使用链接来回复下载文件:

var exec = require('child_process').exec;

var currentWorkingDirectory = '/data', // Folder that contains the sub folders of id#/
    folderToZip             = 'id123123',  // Subfolder name id#, e.g. id123123
    tmpFolderForZips        = '/tmp/', // Folder to store the zip files in
    execString              = "zip -r " + tmpFolderForZips + folderToZip + ".zip " + folderToZip; // Tidy string to put it all together
console.log(execString);

var child = exec(execString, { cwd: currentWorkingDirectory });

child.on('error', function(err) {
    console.log(err);
})

child.on('close', function() {
    // respond with download link to zip file
    console.log('Done Zipping');
});

确保apt-get install zip

相关问题