如何使用Node.js Express后端正确处理文件上传? [英] How to properly handle files upload using Node.js Express backend?

查看:87
本文介绍了如何使用Node.js Express后端正确处理文件上传?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我决定使用 ng-flow flow.js 的Angular实现在前端处理文件上传,然后我选择 multer 作为接收文件的中间件.

I decided to use ng-flow, an Angular implementation of flow.js at front end to handle files uploading, I then picked multer as middleware to receive the files.

我为multer做了最简单的中间件设置:

I did the most simple middleware setup for multer:

app.use(multer({ dest: './temp_uploads/'}))

有一条/POST上传路线,我现在正在记录控制台,以接收所接收的内容:

Got a /POST upload route and I'm now logging to console what's being received:

app.route('/upload').post(function(request,response,next){

    console.log(request.body)
    console.log(request.files)
    // Response code and stuff then ...
});

所以输出是:

{ flowChunkNumber: '1',
  flowChunkSize: '1048576',
  flowCurrentChunkSize: '1606857',
  flowTotalSize: '1606857',
  flowIdentifier: '1606857-IMG_20140807_153553jpg',
  flowFilename: 'IMG_20140807_153553.jpg',
  flowRelativePath: 'IMG_20140807_153553.jpg',
  flowTotalChunks: '1' }
{ file: 
   { fieldname: 'file',
     originalname: 'blob',
     name: 'c12d6f8d4950e48eee21b43f8ee4344a',
     encoding: '7bit',
     mimetype: 'application/octet-stream',
     path: 'temp_uploads/c12d6f8d4950e48eee21b43f8ee4344a',
     extension: '',
     size: 1606857,
     truncated: false,
     buffer: null }}

实际上,所有内容都保存在我的服务器上,应该保存在/temp_uploads 下.但是文件将不会保留其名称,甚至不会扩展名.我不知道该怎么做才能很好地配置它,甚至可以防止服务器出现问题.

Actually everything is being saved at my server, under /temp_uploads as it's supposed to. But files won't keep their names and not even extension. I wonder what I should do to configure it all good and even prevent problems at my server.

以防万一,我将解释我要使用的文件.收到后,我会将它们存储到Google Cloud Storage Platform.然后,我将这些文件作为附件邮寄,如果文件大于15 MB,我将提供一个链接以从邮件中下载.

Just in case, I'll explain what I wanna do with the files. After receiving, I'll store them to Google Cloud Storage Platform. Then I will mail those files as attachments, if files are bigger than 15 MB I'll include a link to download at the mail.

flow.js 网站上,他们显示了此PHP代码段,用于在服务器上处理文件,但是由于我的PHP后台翻译较差,因此不是那么可靠的选择我:

At flow.js site they show this PHP snippet for handling files at server, but as I have a poor PHP background translating code isn't so reliable option to me:

$config = new \Flow\Config();
$config->setTempDir('./chunks_temp_folder');
$file = new \Flow\File($config);

if ($_SERVER['REQUEST_METHOD'] === 'GET') {
    if ($file->checkChunk()) {
        header("HTTP/1.1 200 Ok");
    } else {
        header("HTTP/1.1 404 Not Found");
        return ;
    }
} else {
  if ($file->validateChunk()) {
      $file->saveChunk();
  } else {
      // error, invalid chunk upload request, retry
      header("HTTP/1.1 400 Bad Request");
      return ;
  }
}
if ($file->validateFile() && $file->save('./final_file_name')) {
    // File upload was completed
} else {
    // This is not a final chunk, continue to upload
}

我想使代码更有效,并切实完成可靠的实现工作,我应该如何改进它?

I'd like to make code more effective and actually doing the job making reliable implementation, how should I improve it?

推荐答案

实际上,所有内容都保存在我的服务器上,如预期那样保存在/temp_uploads下.但是文件不会保留其名称,甚至不会扩展.我不知道该怎么做才能很好地配置它,甚至可以防止服务器出现问题.

Actually everything is being saved at my server, under /temp_uploads as it's supposed to. But files won't keep their names and not even extension. I wonder what I should do to configure it all good and even prevent problems at my server.

通过遵循multer README.md中的示例,您可以控制命名.我将其包含在下面:

You can get control over naming by following the example at multer README.md. I've included it below:

app.use(multer({
    dest: './uploads/',
    rename: function (fieldname, filename) {
        return filename.replace(/\W+/g, '-').toLowerCase() + Date.now()
    }
}))

像其他很多钩子一样.

另请参见云存储模块.您可以使用它将数据推送到Google.

See also cloud-storage module. You can use that to push data to Google.

抽象-blob-store

abstract-blob-store and google-cloud-storage built on top of that would be even better fit. It would allow you to stream the files directly to Google without having to use an intermediate storage.

这篇关于如何使用Node.js Express后端正确处理文件上传?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆