node-formidable / formidable

The most used, flexible, fast and streaming parser for multipart form data. Supports uploading to serverless environments, AWS S3, Azure, GCP or the filesystem. Used in production.
MIT License
7.04k stars 682 forks source link

uploadify.com #22

Closed metamindxx closed 13 years ago

metamindxx commented 13 years ago

Hi,

Just been looking at integrating formidable with uploadify and have to go and sort the kids out. Here is where I'm at. Any help would be appreciated.

I have a test page that sends a text file at the moment. The request looks like this:

------------KM7ae0cH2ei4gL6ei4GI3KM7cH2KM7 Content-Disposition: form-data; name="Filename"

test.txt ------------KM7ae0cH2ei4gL6ei4GI3KM7cH2KM7 Content-Disposition: form-data; name="folder"

/uploads ------------KM7ae0cH2ei4gL6ei4GI3KM7cH2KM7 Content-Disposition: form-data; name="Filedata"; filename="test.txt" Content-Type: application/octet-stream

test file contents ------------KM7ae0cH2ei4gL6ei4GI3KM7cH2KM7 Content-Disposition: form-data; name="Upload"

Submit Query ------------KM7ae0cH2ei4gL6ei4GI3KM7cH2KM7--

I'm putting it through this code:

var form = new formidable.IncomingForm(),
    files = [],
    fields = [];

form.uploadDir = 'testTemp/';

form
  .on('field', function(field, value) {
    p([field, value]);
    fields.push([field, value]);
  })
  .on('file', function(field, file) {
    p([field, file]);
    files.push([field, file]);
  })
  .on('end', function() {
    util.puts('-> upload done');
    res.writeHead(200, {'content-type': 'text/plain'});
    res.write('received fields:\n\n '+util.inspect(fields));
    res.write('\n\n');
    res.end('received files:\n\n '+util.inspect(files));
  });
try {
    form.parse(req);
} catch (err) {
    util.inspect(err)
}

and getting this error: [ 'Filename', 'test.txt' ] [ 'folder', '/uploads' ]

events.js:12 throw arguments[1]; // Unhandled 'error' event ^ Error: MultipartParser.end(): stream ended unexpectedly at MultipartParser.end (/home/ed/node/ry-node-73318fa/formidable/multipart_parser.js:311:12) at IncomingMessage. (/home/ed/node/ry-node- 73318fa/formidable/incoming_form.js:80:30) at IncomingMessage.emit (events.js:27:15) at HTTPParser.onMessageComplete (http.js:112:23) at Stream.ondata (http.js:783:22) at Stream._onReadable (net.js:566:27) at IOWatcher.onReadable as callback

metamindxx commented 13 years ago

OK. I put in an error handler:

  .on('error', function(err) {
    prj.logError('failure: ' + util.inspect(err))
    res.writeHead(200, {'content-type': 'text/plain'});
    res.end('error:\n\n'+util.inspect(err));
  })

and it seems to be uploading the files fine. I have checked with binary files and they don't seem to be getting corrupted. I still get the same error though:

stream ended unexpectedly

I have been looking through the ActionScript of the uploader and it doesn't mean a great deal to me. I looks like it is using an inbuilt flash to actually do the upload though (it seems to pick up the AS event):

file.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA, fileCompleteHandler);

Any ideas? If this is the way flex works should I just ignore it?

The multipart_parser state when the error is thrown is "PART_DATA" by the way.

felixge commented 13 years ago

This is a bug in the multipart parser which has been reported before as #6. Since I don't use any flash uploaders I haven't gotten around to fix it yet, but I'll look into it.

metamindxx commented 13 years ago

Cool. I will close this one out then.

Thanks.