Add support for writing binary files to a web server.

This works in current Chromium and Firefox 3.6. It does not work in Opera 10.
This commit is contained in:
Jos van den Oever
2010-11-15 22:42:32 +01:00
parent 7f32fbe561
commit bea5815788
2 changed files with 51 additions and 2 deletions
+18 -1
View File
@@ -9,7 +9,24 @@ var sys = require("sys"),
http.createServer(function (request, response) {
var uri = url.parse(request.url).pathname,
filename = path.join(process.cwd(), uri);
console.log(url + " " + uri + " " + filename);
console.log(request.method + " " + url + " " + uri + " " + filename);
function put() {
var alldata = "";
request.on("data", function (data) {
alldata += data;
});
request.on("end", function () {
fs.writeFile(filename, alldata, "binary", function (err) {
// todo: add error handling
response.writeHead(200);
response.end();
});
});
}
if (request.method === "PUT") {
put(request, response);
return;
}
fs.stat(filename, function (err, stats) {
if (!err && stats.isFile()) {
fs.readFile(filename, "binary", function (err, file) {