Uploading large file to NodeJS webserver

五迷三道 提交于 2020-01-01 03:13:06

问题


I am serving up my web application using NodeJS server (ExpressJS) currently. One of the new requirements is for the users to be able to upload large videos (potentially in gigs) to the server. They will later then be able to download them again. Could I achieve this with the current stack?

I came across tutorials using Multer JS with "multipart/form-data" specified by the front-end. If I use this, would my Express be able to serve up other HTTP requests while writing a giant single video file to the server's disk?


回答1:


Multer should work fine. It'll stream the data as it comes in over HTTP to a file on disk. It won't lock up your server. Then in your endpoint you have access to that file location and can do whatever you need with it.




回答2:


Having done this myself (~20GB files, without multer) I can recommend the following (Probably most of which you have considered, but for completeness):

  1. Choose an appropriate upload control (or write your own, but basically something that chunks up the data is best. I used plupload I think)

  2. On the server make an API to handle the received chunk data, and write it out to a temp location during upload (You may want to hash and check individual pieces as well).

  3. Once upload complete, check file (I used a hash generated by the client, and checked it against the uploaded data)




回答3:


    var express =   require("express");

    var app =   express();
    var async = require('async');
    var fs = require('fs');
    var client = redis.createClient();
    var multiparty = require('multiparty');
    var util = require('util');

    app.post('/',function(req,res){

        var form = new multiparty.Form();

        form.parse(req, function(err, fields, files) {

             client.get(fields.otp, function(err, reply) {
    var oldpath = files.file[0].path;
                          var newpath = path + files.file[0].originalFilename;
                          fs.rename(oldpath, newpath, function (err) {
                            if (err) throw err;
                            res.write('File uploaded and moved!');
                            res.end();
                          });
    }
    }



app.listen(2001,function(){
    console.log("Server is running on port 2001");
});


来源:https://stackoverflow.com/questions/40723536/uploading-large-file-to-nodejs-webserver

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