Re: [nodejs] express and formidable and restful server and sending a file from a REST client

Showing 1-2 of 2 messages
Re: [nodejs] express and formidable and restful server and sending a file from a REST client Gustavo Machado 10/3/12 1:34 PM
Maybe this can help. When trying to use ajax uploader and also plain forms, I found that I had to do something like this it's coffeescript):

        if req.xhr
            req.tempFile = path.join(tmp.tmpdir, req.header("x-file-name"))
            writer = fs.createWriteStream(req.tempFile)
            req.pipe writer
            writer.on "close", () ->
                next()
        else
            req.tempFile = req.files[Object.keys(req.files)[0]]
            next()

Hope it helps!
Gus

On Wed, Oct 3, 2012 at 1:09 PM, BlondAngelNYC <paul...@gmail.com> wrote:
I built a restful server based on express (and express relies on formidable). I used this form to perform my test:
<form action="/docxv" method="post" enctype="multipart/form-data">
 <input type="submit" value="Upload">
 <input type="file" name="uploadfile">
</form>
 
However customer wants the REST client to send the file to the restful server *WITHOUT* using multipart/form-data.  So I changed the form to be this:
<form action="/docxv" method="post">
 <input type="submit" value="Upload">
 <input type="file" name="uploadfile">
</form>
 
That way, the form is sent as UUEncoded instead of multipart. 
 
My restful server javascript code (I am listing only the important bits):
var app = express();
app.configure(function () {
  app.use(express.bodyParser({keepExtensions: true, hash:'md5' }));
  app.use(express.methodOverride());
  app.use(app.router);
  app.use(express.static(path.join(application_root, "public")));
  app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
//router POST /docxv (upload a file and generate a new ID)
app.post(uploadform, function (req, res){
  uploadfileMultipart(req, res, 0);
});
//start the server.
app.listen(port);
logger.info('Server running on port ' + port);
//from POST router
function uploadfileMultipart(req, res, id) {
 var keys = Object.keys(req.files);
 var currentFile = req.files[keys[0]];
 var data = fs.readFileSync(currentFile.path,, 'base64'); 
 //...etc...
 
}
 
 
The problem is -- when changing from multipart to non-multipart, the req.files parameter no longers works correctly.   So, I was thinking that I should try to get the raw data from request but I don't have an easy way to do that...  However I found tantalizing tidbits but no solid examples.  One such tidbit:
So I added that to my code like this within the app.configure snippet:
app.configure(function () {
  app.use(function(req, res, next) {
    var data='';
    req.setEncoding('utf8');
    req.on('data', function(chunk) {
     data += chunk;
    });
    req.on('end', function() {
     req.rawBody=data;
     next();
    });
  });
  app.use(app.router);
  app.use(express.static(path.join(application_root, "public")));
  app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
 
Unfortunately, when running this, either it hangs (when using UUEncode) or that req.rawBody is undefined (when using multipart).  I am a noob, and I *did* search but not much luck.  I know about the incoming.onForm function, but there are no solid examples for me to follow while using ExpressJS (which in turn uses Formidable).
 
Any help would be appreciated.

--
Job Board: http://jobs.nodejs.org/
Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
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?hl=en

Re: [nodejs] express and formidable and restful server and sending a file from a REST client BlondAngelNYC 10/5/12 7:36 AM
have yet to try coffeescript.  I know that this will compile to straight javascript. But I am working with ExpressJS and I doubt that the following code would work.