I'm adding photo uploading feature to my chat. I have POST parser from this answer, and little code that saves image to uploads directory.
var ext = exports.getFileExt(Object.keys(partsByName)[0]);
var time = (new Date()).getTime();
fs.writeFile("./img/user/upload_" + time + "." + ext,
partsByName[Object.keys(partsByName)[0]], "binary", function (err) {
if (!err) {
res.writeHead(200, "OK", { "Content-Type": "text/html" });
res.write(exports.wrapHTML(<title>, <html>, <font params>));
res.end();
} else {
console.error(err);
res.writeHead(500, "Internal Server Error");
res.end();
}
});
Image saves with expected name, but it doesn't opens and I think there is incorrect encoding.
Source file start looks like this:
яШяа JFIF H H яб"Exif MM *
but newly saved file is different:
ээээ JFIF H H ээ"Exif MM *
I tried writing my own parser before I found that answer, and it has other structure - it extracted boundary with /boundary=(.+)/
regex, then getting file extension from parsed file name, and cutting 2 first lines with array, then joining with "\r\n", and applying substr(boundary.length, data.length - (boundary.length * 2))
(and some other corrections), saving with fs.writeFile
, and file again looked incorrectly.
UPD: Now I think that Cyrillic just lost when sending data. In console and browser they look as unknown character (stroked rectangle).