I think the image is corrupted because you're calling posix.write (and
posix.close asynchronously after that) on the "body" event of the
stream, rather than the "complete" event.
Here is some sample upload code (modified from the multipart sample in
the docs) that might help:
// inside http.createServer callback
if (req.uri.path == "/upload") {
var stream = new multipart.Stream(req);
var parts = {}, names = {}, current_upload = {};
stream.addListener("part", function (part) {
var buffer = "";
part.addListener("body", function (chunk) {
buffer = buffer + chunk;
sys.puts(JSON.stringify(current_upload));
});
part.addListener("complete", function () {
parts[
part.name] = buffer;
if (part.filename) {
names[
part.name] = part.filename;
}
});
});
stream.addListener("complete", function() {
for (part in parts) {
uploader.create_and_write_file(names[part],parts[part]);
res.sendBody("upload complete");
res.finish();
};
});
}
var create_and_write_file = function (filename,data) {
var write_data = function (descriptor) {
var write_promise = posix.write(descriptor, data, null, "binary")
write_promise.addCallback(function (bytes) {
sys.puts(bytes + " bytes written");
posix.close(descriptor);
});
write_promise.addErrback(function () {
posix.close(descriptor);
sys.puts("an error");
})
};
posix.open(filename,process.O_CREAT |
process.O_RDWR,0755).addCallback(write_data);
};
You could also put the create_and_write_file function in the callback
for the part complete callback, if desired.
Cheers,
Andrew
> --
>
> You received this message because you are subscribed to the Google Groups "nodejs" group.
> To post to this group, send email to
nod...@googlegroups.com.
> To unsubscribe from this group, send email to
nodejs+un...@googlegroups.com.
> For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en.
>
>
>
--
Andrew Lunny
Software Developer, Nitobi
604 685 9287
andrew...@nitobi.com