How to properly handle file uploads with Node.js Express backend?
I decided to use , Angular's implementation to handle file uploads, then I chose as middleware to get files. ng-flow
flow.js
multer
I did the simplest intermediate install for multer:
app.use(multer({ dest: './temp_uploads/'}))
Got the route /POST upload
and now I log the console to get this:
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 saved on my server under /temp_uploads
as intended. But the files will not contain their names and not even the extension . I wonder what I have to do to set this up permanently and even prevent problems on my server.
Just in case, I'll explain what I want to do with the files. Once received, I will store them on the Google Cloud Storage Platform. Then I will send these files as attachments, if the files are more than 15MB, I will include a download link by mail.
On the site, they show this PHP snippet for processing files on the server, but since I have bad PHP background translation code, this is not that reliable option for me: flow.js
$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 would like to make the code more efficient and actually work on a robust implementation, how can I improve it?
source to share
Actually everything is saved on my server under / temp_uploads as intended. But the files won't keep their names or even expand. I wonder what I have to do to set this up permanently and even prevent problems on my server.
You can gain control over the naming by following the multer README.md example. I have included it below:
app.use(multer({
dest: './uploads/',
rename: function (fieldname, filename) {
return filename.replace(/\W+/g, '-').toLowerCase() + Date.now()
}
}))
It looks like there are other hooks as well.
See also cloud-storage module. You can use this to transfer data to Google.
abstract-blob-store and google-cloud-storage based on top of this would be even better. This will allow you to directly transfer files to Google without using intermediate storage.
source to share