How to properly handle files upload using Node.js Express backend?

我与影子孤独终老i 提交于 2021-01-27 13:57:13

问题


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.

I did the most simple middleware setup for multer:

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

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 ...
});

So the outputs are :

{ 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 }}

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.

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.

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?


回答1:


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.

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()
    }
}))

Looks like there are many other hooks as well.

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

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.



来源:https://stackoverflow.com/questions/26452950/how-to-properly-handle-files-upload-using-node-js-express-backend

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!