5

I have a webpage with a form, the form has a file input, and when I submit the form I would like the server to write it to a file server side.

I have tried uploading several test images using this form, and so far every one turns out as a plain text file containing the original file's filename. I have found no good information or examples of how to do this online except for "use Express!", "use This!", "use That!", "use The Other Thing!" and it's quite aggravating.

6
  • 6
    Don't tell others what not to tell you when you come to them for advice. If you so firmly know what you want, then simply do it and ask nobody. File transfer via HTTP is a solved problem. It's not as trivial as you would think, and libraries have been written that do the heavy lifting. If you want to know how they do it, look at their source code. Don't ask people to write yet another library just for you, only because you can't be bothered with them yourself. Commented Mar 21, 2015 at 15:46
  • 2
    I never asked for someone to write a library. I asked for "pointers, links, or help." I also respectully requested that no one tell me to use a library, I clearly clearly separated the last part of my post labelling it as a request and explaining why I would not like anyone to simply tell me to use a library. To me, if I use a library, I don't learn how it works, I use it as a crutch to get the task done standing atop others work. I want to know how it works and write my own code, however I obviously don't know what I need to. Thus the request for "pointers, links, or help." Commented Mar 21, 2015 at 16:13
  • 1
    I know it's frustrating to bang your head against a problem for five days, but keep in mind that no one on SO is obligated to answer your question or help you. No one is getting paid for it. So to come in, guns blazing, saying that there are a lot of "cop out" answers on SO might not be the best approach. Commented Mar 21, 2015 at 16:19
  • Hours of searching and seeing the same answers that never helped was upsetting. I apologize for that terminology, I didn't mean to offend anyone, simply point out my dislike for those types of answers. I wanted my question to be read literally without anyone referring me to use someone elses code. I wish to learn the how and make it myself. Commented Mar 21, 2015 at 16:27
  • You don’t have to use a framework, but I really would recommend a module. Multipart encoding is annoying to parse, and busboy does that and only that. You can read its source for inspiration, but it’ll be tedious to reimplement. Commented Mar 21, 2015 at 17:55

1 Answer 1

5

I agree with what Tomalak said in the comments; the problem you're trying to solve with Node alone and no libraries is not trivial, and requires a pretty thorough understanding of multiple different technologies.

That said, I'll help get you started, and hopefully this will be a good jumping-off point for reading source code of the modules that do do this.

Using nothing but Node, here's a minimal server that will log to the console the body of any request (I do not recommend sending a big file to this, or a binary file...start with text files):

var http = require('http');

var server = http.createServer(function(req, res) {
    if(req.url === '/favicon.ico') return res.statusCode = 404, res.end();
    console.log(req.method + ' ' + req.url);
    var data = '';
    req.on('data', function(chunk) {
        data += chunk;
    });
    req.on('end', function() {
        console.log(data);
    });
    res.end('done');
});

server.listen(3000, function() {
    console.log('listening on 3000');
});

Then you can create an HTML form to connect to it:

<!doctype html>
<html>
<head>
</head>
<body>
    <form action="http://localhost:3000" method="POST" enctype="multipart/form-data">
        <input type="file" name="f">
        <input type="submit">
    </form>
</body>
</html>

Parsing multipart/form-data is not a trivial task, but you can start by reading the documentation here:

http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4

If you want to look at someone's implementation of a multipart/form-data parser, this one is pretty solid:

https://www.npmjs.com/package/dicer

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

3 Comments

Thank you. I will use your recommendation and see where I get. Thank you especially for the w3 link. I also apologize if my tone was poor. I was quite annoyed so I tried to explain myself in the question. When you don't know something and can't find where or how to learn it, it becomes quite upsetting. I apologize again.
Thank you for the dicer link as well. I just saw that update.
I've been there, FatalKeystroke, believe me. Just remember that SO isn't the place to vent your frustrations -- the community will shut that down pretty quickly.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.