Frage

So I have been wondering how to save an image to node.js server without the use of express.(just learning node and want to make everything myself to learn node better, without express).

So far I have a form with the image as only input which I send with a post request. This is what I have so far on my server, which does not log anything.

if(req.method === 'POST') {

    if (req.url === '/upload') {

        req.on('error', function(e) {
          console.log('Problem with request: ' + e.message);
        });

        res.writeHead(200, {'Content-Type': 'image/jpg; charset=utf8'});

        req.on('data', function (chunk) {

            fs.writeFile(__dirname + "/uploads/dada.jpg", chunk, function(err) {
                if(err) {
                    console.log(err);
                } else {
                    console.log("The file was saved!");
                }
            }); 
        });
    }
}

This is my form:

<form method="post" action="/upload" enctype="multipart/form-data">

EDIT: I fixed most of the problems, but the file is only saved as an image, but cannot be viewed like one. (Something is wrong with the content-type I guess, but don't know how to fix it) Here is fiddle of my whole app. I know I need to separate it in different modules, but I will do that later

War es hilfreich?

Lösung

I completely forgot about this old question but now that I see it has quite some views, here is the solution I found:

var port = 1357;

var http = require('http'),
    path = require('path'),
    mime = require('mime'),
    fs = require('fs'),
    GUID = require('GUID'),
    formidable = require('formidable'),
    util = require('util');

var app = http.createServer(function (req, res) {

    if (req.method === 'POST') {

        if (req.url === '/upload') {

            req.on('error', function (e) {
                console.log('Problem with request: ' + e.message);
            });

            var fileDirectory = __dirname + '/db/',
                form = new formidable.IncomingForm();

            form.keepExtensions = true;
            form.uploadDir =fileDirectory;

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

                if (err) throw (err);

                var pic = JSON.stringify(util.inspect(files)),
                    upIndx = pic.indexOf('db'),
                    path = pic.slice(upIndx + 6, upIndx + 42);

                res.writeHead(200, {
                    'Content-Type': 'text/html'
                });
                fs.readFile('views/index.html', function (err, page) {
                    res.writeHead(200, {
                        'Content-Type': 'text/html'
                    });
                    res.write(page);
                    res.write('<div>Download Link: </div><div>' + fileDirectory + path + '</div>');
                    res.end();
                });
            });

        }
    } else {

        //not important for question, handle other request
    }

});

app.listen(port);
console.log('Server running on port: ' + port)
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top