2

I'm using multiparty for uploading a file; I'm so new to Node.JS and streaming; so my question is, is it right if I stream the file by the file.path which is returned in form.parse() like the way I'm doing in my attempted code? I mean this is absolute path and obviously is working on localhost because it is the absolute path of my current server which is localhost, but is it going to work when the user attempts to upload a file from their computer too?

form.parse(req, function (err, fields, files) {
    var rs= fs.createReadStream(files.file[0].path);
    var fileDate;
    rs.on('readable', function () {
        while (null !== (chunk = rs.read())) {
            fileDate += chunk;
        }
    });

    rs.on('end', function () {
        console.log('importedData', fileDate);
    });
});

Thanks, please let me know if you need more clarification!

1 Answer 1

1

That looks correct. By default, uploaded files are put in a temporary folder, if you're using Linux this will likely be /tmp, your users' files will end up in the same place when they upload their files through your front-end.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks man! But in fact I should have mentioned earlier that I'm not exactly uploading the file to the server; in fact in the front-end user is passing a file in form-data and in the back-end I'm just reading the file and use the data of the file and not storing it anywhere in the server! Exactly like form-data in POSTMAN and passing file as a file format, where you can browse and add a file; so based on what you described still a temporary file automatically will be stored in the server (which is Linux environment) in somewhere like /tmp, right? and file.path should work? right? Thanks again
@user3399784 multiparty allows you to add file and field events to get the form data without saving to disk first. This also requires you to not pass in a callback to form.parse().
@mscdex So if I understood correctly now that I'm using a callback in form.parse() I'm saving the file on disk, right? And although my code works since data will be saved on the hard disk on the server and file.path is valid in my example code, this is not a recommended way and I should avoid saving the file on Hard Disk, right?
@mscdex So I wonder if I dont use callback in form.parse() to save the file in a temporary place, what is the right way to read the file, I thought for reading the file I need to pass the path (sorry I'm new to Node.JS) , could you please help me how I can read the file if the file is not saved on the hard drive and I dont have the path? In other words is there a way to pipe the stream to a variable in form.on('part',function(part) {... ?Thanks so much!!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.