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.01k stars 680 forks source link

Error: MultipartParser.end(): stream ended unexpectedly: state = START #79 #298

Closed macatadobe closed 3 years ago

macatadobe commented 9 years ago

I am new to node.js . I have created a node.js server which accepts post requests . I am using phonegap android application to send image on local server.

error: Error: MultipartParser.end(): stream ended unexpectedly: state = START

Snippet of code is as shown below . My form.on('end' ... is not getting called and there by my image is not getting uploaded on to the server

app.post('/upload', function(req, res) {

        console.log("Upload Code Hit!")

        var form = new formidable.IncomingForm();
        form.parse(req, function(err, fields, files) {
                        console.log("Upload Code Hit2222!")
                        res.writeHead(200, {'content-type': 'text/plain'});
                        res.write('received upload:\n\n');

                        res.end(util.inspect({fields: fields, files: files}));

                        console.log(" form. parse ends ")
                });
        console.log("33333!")
        form.on('end', function(fields, files) {
                console.log(" form.in(end) hit ")
                /* Temporary location of our uploaded file */
                var temp_path = this.openedFiles[0].path;
                /* The file name of the uploaded file */

                var file_name=this.openedFiles[0].name;
                /* Location where we want to copy the uploaded file */
                var new_location = './uploadedFOlder/';

                fs.copy(temp_path, new_location + file_name, function(err) {
                        if (err) {
                        console.error(err);
                        } else {
                        console.log("success!")
                        }
                        });
                });

                console.log("88888")

return;

});

i got Upload Code Hit! 33333! 88888 Upload Code Hit2222! form. parse ends ie I am not getting the log

console.log(" form.in(end) hit ")

I added a code

form.on('error', function(err) { console.log('ERROR!'+ err); res.end(); }); and it shows

ERROR!Error: MultipartParser.end(): stream ended unexpectedly: state = START can some one please help me what i am doing wrong ?

OrangeDog commented 9 years ago

Probably because you're not sending a multipart form to the server, but perhaps just the image directly?

macatadobe commented 9 years ago

i am sending multipart form to the server

OrangeDog commented 9 years ago

Prove it.

macatadobe commented 9 years ago

i am using phone gap android sample program from net . phonegap documentation says "The FileTransfer object provides a way to upload files to a remote server using an HTTP multi-part POST request." . since i am using FileTransfer request , hence i feel multipart form will be sent .

my phonegap code

    cameraOPtions={
            quality: 50,
            destinationType: navigator.camera.DestinationType.FILE_URI,
            sourceType:navigator.camera.PictureSourceType.CAMERA 
            };

    navigator.camera.getPicture(
                                 function (imageURI) 
                                 {
                                     alert('image captured , Lets try to send it over network') ;

                                     var ft = new FileTransfer(),
                                        options = new FileUploadOptions();
                                        options.fileKey = "file";
                                        options.fileName = 'filename.jpg'; // We will use the name auto-generated by Node at the server side.
                                        options.mimeType = "image/jpeg";
                                        options.chunkedMode = false;
                                        options.params = { // Whatever you populate options.params with, will be available in req.body at the server-side.
                                            "description": "Uploaded from my phone"
                                        };

; var serverURL="http:\/\/192.168.137.135:3000\/upload"; ft.upload(imageURI, serverURL , function (e) { alert("File upload sucess");

                                            },
                                            function (error) {
                                                alert("Upload failed");
                                                alert("An error has occurred in upload : Code = [" + error.code +  "]  error.source [" + error.source 
                                                        +"]   error.target: [" +error.target + "] ");
                                                console.log("upload error source " + error.source);
                                                console.log("upload error target " + error.target);

                                            }, options);

                                 },
                                 function(errorCode)
                                 {
                                     alert ('some error');
                                    //alert(Failed because:  + errorCode.message);
                                 },
                                 cameraOPtions
                                 ); 
OrangeDog commented 9 years ago

Hmm. Instead of passing the request to formidable, can you just log the request body instead (with a teeny tiny test image)?

macatadobe commented 9 years ago

when i dumped by req it came out to be too big .. but here it is anyways Express server listening on port 3000 Upload Code Hit!

req
{ _readableState: 
   { highWaterMark: 16384,
     buffer: [],
     length: 0,
     pipes: null,
     pipesCount: 0,
     flowing: false,
     ended: true,
     endEmitted: true,
     reading: false,
     calledRead: true,
     sync: false,
     needReadable: true,
     emittedReadable: false,
     readableListening: false,
     objectMode: false,
     defaultEncoding: 'utf8',
     ranOut: false,
     awaitDrain: 0,
     readingMore: false,
     decoder: null,
     encoding: null },
  readable: false,
  domain: null,
  _events: 
   { error: [Function],
     aborted: [Function],
     data: [Function],
     readable: [Function],
     end: [Function] },
  _maxListeners: 10,
  socket: 
   { _connecting: false,
     _handle: 
      { fd: 19,
        writeQueueSize: 0,
        owner: [Circular],
        onread: [Function: onread],
        reading: true },
     _readableState: 
      { highWaterMark: 16384,
        buffer: [],
        length: 0,
        pipes: null,
        pipesCount: 0,
        flowing: false,
        ended: false,
        endEmitted: false,
        reading: true,
        calledRead: true,
        sync: false,
        needReadable: true,
        emittedReadable: false,
        readableListening: false,
        objectMode: false,
        defaultEncoding: 'utf8',
        ranOut: false,
        awaitDrain: 0,
        readingMore: false,
        decoder: null,
        encoding: null },
     readable: true,
     domain: null,
     _events: 
      { end: [Object],
        finish: [Function: onSocketFinish],
        _socketEnd: [Function: onSocketEnd],
        drain: [Object],
        timeout: [Function],
        error: [Function],
        close: [Object] },
     _maxListeners: 10,
     _writableState: 
      { highWaterMark: 16384,
        objectMode: false,
        needDrain: false,
        ending: false,
        ended: false,
        finished: false,
        decodeStrings: false,
        defaultEncoding: 'utf8',
        length: 0,
        writing: false,
        sync: true,
        bufferProcessing: false,
        onwrite: [Function],
        writecb: null,
        writelen: 0,
        buffer: [] },
     writable: true,
     allowHalfOpen: true,
     onend: [Function],
     destroyed: false,
     errorEmitted: false,
     bytesRead: 13582,
     _bytesDispatched: 0,
     _pendingData: null,
     _pendingEncoding: '',
     server: 
      { domain: null,
        _events: [Object],
        _maxListeners: 10,
        _connections: 1,
        connections: [Getter/Setter],
        _handle: [Object],
        _usingSlaves: false,
        _slaves: [],
        allowHalfOpen: true,
        httpAllowHalfOpen: false,
        timeout: 120000,
        _connectionKey: '4:0.0.0.0:3000' },
     _idleTimeout: 120000,
     _idleNext: { _idleNext: [Circular], _idlePrev: [Circular] },
     _idlePrev: { _idleNext: [Circular], _idlePrev: [Circular] },
     _idleStart: 1408032988469,
     parser: 
      { _headers: [],
        _url: '',
        onHeaders: [Function: parserOnHeaders],
        onHeadersComplete: [Function: parserOnHeadersComplete],
        onBody: [Function: parserOnBody],
        onMessageComplete: [Function: parserOnMessageComplete],
        socket: [Circular],
        incoming: [Circular],
        maxHeaderPairs: 2000,
        onIncoming: [Function] },
     ondata: [Function],
     _paused: false,
     _httpMessage: 
      { domain: null,
        _events: [Object],
        _maxListeners: 10,
        output: [],
        outputEncodings: [],
        writable: true,
        _last: false,
        chunkedEncoding: false,
        shouldKeepAlive: true,
        useChunkedEncodingByDefault: true,
        sendDate: true,
        _headerSent: false,
        _header: '',
        _hasBody: true,
        _trailer: '',
        finished: false,
        _hangupClose: false,
        socket: [Circular],
        connection: [Circular],
        app: [Object],
        _headers: [Object],
        _headerNames: [Object],
        req: [Circular],
        viewCallbacks: [],
        locals: [Function: locals] } },
  connection: 
   { _connecting: false,
     _handle: 
      { fd: 19,
        writeQueueSize: 0,
        owner: [Circular],
        onread: [Function: onread],
        reading: true },
     _readableState: 
      { highWaterMark: 16384,
        buffer: [],
        length: 0,
        pipes: null,
        pipesCount: 0,
        flowing: false,
        ended: false,
        endEmitted: false,
        reading: true,
        calledRead: true,
        sync: false,
        needReadable: true,
        emittedReadable: false,
        readableListening: false,
        objectMode: false,
        defaultEncoding: 'utf8',
        ranOut: false,
        awaitDrain: 0,
        readingMore: false,
        decoder: null,
        encoding: null },
     readable: true,
     domain: null,
     _events: 
      { end: [Object],
        finish: [Function: onSocketFinish],
        _socketEnd: [Function: onSocketEnd],
        drain: [Object],
        timeout: [Function],
        error: [Function],
        close: [Object] },
     _maxListeners: 10,
     _writableState: 
      { highWaterMark: 16384,
        objectMode: false,
        needDrain: false,
        ending: false,
        ended: false,
        finished: false,
        decodeStrings: false,
        defaultEncoding: 'utf8',
        length: 0,
        writing: false,
        sync: true,
        bufferProcessing: false,
        onwrite: [Function],
        writecb: null,
        writelen: 0,
        buffer: [] },
     writable: true,
     allowHalfOpen: true,
     onend: [Function],
     destroyed: false,
     errorEmitted: false,
     bytesRead: 13582,
     _bytesDispatched: 0,
     _pendingData: null,
     _pendingEncoding: '',
     server: 
      { domain: null,
        _events: [Object],
        _maxListeners: 10,
        _connections: 1,
        connections: [Getter/Setter],
        _handle: [Object],
        _usingSlaves: false,
        _slaves: [],
        allowHalfOpen: true,
        httpAllowHalfOpen: false,
        timeout: 120000,
        _connectionKey: '4:0.0.0.0:3000' },
     _idleTimeout: 120000,
     _idleNext: { _idleNext: [Circular], _idlePrev: [Circular] },
     _idlePrev: { _idleNext: [Circular], _idlePrev: [Circular] },
     _idleStart: 1408032988469,
     parser: 
      { _headers: [],
        _url: '',
        onHeaders: [Function: parserOnHeaders],
        onHeadersComplete: [Function: parserOnHeadersComplete],
        onBody: [Function: parserOnBody],
        onMessageComplete: [Function: parserOnMessageComplete],
        socket: [Circular],
        incoming: [Circular],
        maxHeaderPairs: 2000,
        onIncoming: [Function] },
     ondata: [Function],
     _paused: false,
     _httpMessage: 
      { domain: null,
        _events: [Object],
        _maxListeners: 10,
        output: [],
        outputEncodings: [],
        writable: true,
        _last: false,
        chunkedEncoding: false,
        shouldKeepAlive: true,
        useChunkedEncodingByDefault: true,
        sendDate: true,
        _headerSent: false,
        _header: '',
        _hasBody: true,
        _trailer: '',
        finished: false,
        _hangupClose: false,
        socket: [Circular],
        connection: [Circular],
        app: [Object],
        _headers: [Object],
        _headerNames: [Object],
        req: [Circular],
        viewCallbacks: [],
        locals: [Function: locals] } },
  httpVersion: '1.1',
  complete: true,
  headers: 
   { 'content-type': 'multipart/form-data; boundary=+++++',
     'user-agent': 'Dalvik/1.6.0 (Linux; U; Android 4.4; Android SDK built for x86 Build/KRT16M)',
     host: '192.168.1.109:3000',
     connection: 'Keep-Alive',
     'accept-encoding': 'gzip',
     'content-length': '13320' },
  trailers: {},
  _pendings: [],
  _pendingIndex: 0,
  url: '/upload',
  method: 'POST',
  statusCode: null,
  client: 
   { _connecting: false,
     _handle: 
      { fd: 19,
        writeQueueSize: 0,
        owner: [Circular],
        onread: [Function: onread],
        reading: true },
     _readableState: 
      { highWaterMark: 16384,
        buffer: [],
        length: 0,
        pipes: null,
        pipesCount: 0,
        flowing: false,
        ended: false,
        endEmitted: false,
        reading: true,
        calledRead: true,
        sync: false,
        needReadable: true,
        emittedReadable: false,
        readableListening: false,
        objectMode: false,
        defaultEncoding: 'utf8',
        ranOut: false,
        awaitDrain: 0,
        readingMore: false,
        decoder: null,
        encoding: null },
     readable: true,
     domain: null,
     _events: 
      { end: [Object],
        finish: [Function: onSocketFinish],
        _socketEnd: [Function: onSocketEnd],
        drain: [Object],
        timeout: [Function],
        error: [Function],
        close: [Object] },
     _maxListeners: 10,
     _writableState: 
      { highWaterMark: 16384,
        objectMode: false,
        needDrain: false,
        ending: false,
        ended: false,
        finished: false,
        decodeStrings: false,
        defaultEncoding: 'utf8',
        length: 0,
        writing: false,
        sync: true,
        bufferProcessing: false,
        onwrite: [Function],
        writecb: null,
        writelen: 0,
        buffer: [] },
     writable: true,
     allowHalfOpen: true,
     onend: [Function],
     destroyed: false,
     errorEmitted: false,
     bytesRead: 13582,
     _bytesDispatched: 0,
     _pendingData: null,
     _pendingEncoding: '',
     server: 
      { domain: null,
        _events: [Object],
        _maxListeners: 10,
        _connections: 1,
        connections: [Getter/Setter],
        _handle: [Object],
        _usingSlaves: false,
        _slaves: [],
        allowHalfOpen: true,
        httpAllowHalfOpen: false,
        timeout: 120000,
        _connectionKey: '4:0.0.0.0:3000' },
     _idleTimeout: 120000,
     _idleNext: { _idleNext: [Circular], _idlePrev: [Circular] },
     _idlePrev: { _idleNext: [Circular], _idlePrev: [Circular] },
     _idleStart: 1408032988469,
     parser: 
      { _headers: [],
        _url: '',
        onHeaders: [Function: parserOnHeaders],
        onHeadersComplete: [Function: parserOnHeadersComplete],
        onBody: [Function: parserOnBody],
        onMessageComplete: [Function: parserOnMessageComplete],
        socket: [Circular],
        incoming: [Circular],
        maxHeaderPairs: 2000,
        onIncoming: [Function] },
     ondata: [Function],
     _paused: false,
     _httpMessage: 
      { domain: null,
        _events: [Object],
        _maxListeners: 10,
        output: [],
        outputEncodings: [],
        writable: true,
        _last: false,
        chunkedEncoding: false,
        shouldKeepAlive: true,
        useChunkedEncodingByDefault: true,
        sendDate: true,
        _headerSent: false,
        _header: '',
        _hasBody: true,
        _trailer: '',
        finished: false,
        _hangupClose: false,
        socket: [Circular],
        connection: [Circular],
        app: [Object],
        _headers: [Object],
        _headerNames: [Object],
        req: [Circular],
        viewCallbacks: [],
        locals: [Function: locals] } },
  _consuming: true,
  _dumped: false,
  httpVersionMajor: 1,
  httpVersionMinor: 1,
  upgrade: false,
  originalUrl: '/upload',
  _parsedUrl: 
   { protocol: null,
     slashes: null,
     auth: null,
     host: null,
     port: null,
     hostname: null,
     hash: null,
     search: null,
     query: null,
     pathname: '/upload',
     path: '/upload',
     href: '/upload' },
  query: {},
  app: 
   { [Function: app]
     use: [Function],
     handle: [Function],
     listen: [Function],
     setMaxListeners: [Function],
     emit: [Function],
     addListener: [Function],
     on: [Function],
     once: [Function],
     removeListener: [Function],
     removeAllListeners: [Function],
     listeners: [Function],
     route: '/',
     stack: [ [Object], [Object], [Object], [Object], [Object], [Object] ],
     init: [Function],
     defaultConfiguration: [Function],
     engine: [Function],
     param: [Function],
     set: [Function],
     path: [Function],
     enabled: [Function],
     disabled: [Function],
     enable: [Function],
     disable: [Function],
     configure: [Function],
     get: [Function],
     post: [Function],
     put: [Function],
     head: [Function],
     delete: [Function],
     options: [Function],
     trace: [Function],
     copy: [Function],
     lock: [Function],
     mkcol: [Function],
     move: [Function],
     propfind: [Function],
     proppatch: [Function],
     unlock: [Function],
     report: [Function],
     mkactivity: [Function],
     checkout: [Function],
     merge: [Function],
     'm-search': [Function],
     notify: [Function],
     subscribe: [Function],
     unsubscribe: [Function],
     patch: [Function],
     all: [Function],
     del: [Function],
     render: [Function],
     request: {},
     response: {},
     cache: {},
     settings: 
      { 'x-powered-by': true,
        env: 'development',
        views: '/Users/manish.agarwal/Mobile_Game_creation/nodeJsProject/ANALYZE/server/node-login/views',
        'jsonp callback name': 'callback',
        'json spaces': 2,
        port: 3000,
        'view engine': 'jade' },
     engines: {},
     viewCallbacks: [],
     _events: { mount: [Function] },
     _router: 
      { map: [Object],
        params: {},
        _params: [],
        caseSensitive: false,
        strict: false,
        middleware: [Function: router] },
     routes: { post: [Object], get: [Object], put: [Object], delete: [Object] },
     router: [Getter],
     locals: { [Function: locals] settings: [Object] },
     _usedRouter: true },
  res: 
   { domain: null,
     _events: { finish: [Function] },
     _maxListeners: 10,
     output: [],
     outputEncodings: [],
     writable: true,
     _last: false,
     chunkedEncoding: false,
     shouldKeepAlive: true,
     useChunkedEncodingByDefault: true,
     sendDate: true,
     _headerSent: false,
     _header: '',
     _hasBody: true,
     _trailer: '',
     finished: false,
     _hangupClose: false,
     socket: 
      { _connecting: false,
        _handle: [Object],
        _readableState: [Object],
        readable: true,
        domain: null,
        _events: [Object],
        _maxListeners: 10,
        _writableState: [Object],
        writable: true,
        allowHalfOpen: true,
        onend: [Function],
        destroyed: false,
        errorEmitted: false,
        bytesRead: 13582,
        _bytesDispatched: 0,
        _pendingData: null,
        _pendingEncoding: '',
        server: [Object],
        _idleTimeout: 120000,
        _idleNext: [Object],
        _idlePrev: [Object],
        _idleStart: 1408032988469,
        parser: [Object],
        ondata: [Function],
        _paused: false,
        _httpMessage: [Circular] },
     connection: 
      { _connecting: false,
        _handle: [Object],
        _readableState: [Object],
        readable: true,
        domain: null,
        _events: [Object],
        _maxListeners: 10,
        _writableState: [Object],
        writable: true,
        allowHalfOpen: true,
        onend: [Function],
        destroyed: false,
        errorEmitted: false,
        bytesRead: 13582,
        _bytesDispatched: 0,
        _pendingData: null,
        _pendingEncoding: '',
        server: [Object],
        _idleTimeout: 120000,
        _idleNext: [Object],
        _idlePrev: [Object],
        _idleStart: 1408032988469,
        parser: [Object],
        ondata: [Function],
        _paused: false,
        _httpMessage: [Circular] },
     app: 
      { [Function: app]
        use: [Function],
        handle: [Function],
        listen: [Function],
        setMaxListeners: [Function],
        emit: [Function],
        addListener: [Function],
        on: [Function],
        once: [Function],
        removeListener: [Function],
        removeAllListeners: [Function],
        listeners: [Function],
        route: '/',
        stack: [Object],
        init: [Function],
        defaultConfiguration: [Function],
        engine: [Function],
        param: [Function],
        set: [Function],
        path: [Function],
        enabled: [Function],
        disabled: [Function],
        enable: [Function],
        disable: [Function],
        configure: [Function],
        get: [Function],
        post: [Function],
        put: [Function],
        head: [Function],
        delete: [Function],
        options: [Function],
        trace: [Function],
        copy: [Function],
        lock: [Function],
        mkcol: [Function],
        move: [Function],
        propfind: [Function],
        proppatch: [Function],
        unlock: [Function],
        report: [Function],
        mkactivity: [Function],
        checkout: [Function],
        merge: [Function],
        'm-search': [Function],
        notify: [Function],
        subscribe: [Function],
        unsubscribe: [Function],
        patch: [Function],
        all: [Function],
        del: [Function],
        render: [Function],
        request: {},
        response: {},
        cache: {},
        settings: [Object],
        engines: {},
        viewCallbacks: [],
        _events: [Object],
        _router: [Object],
        routes: [Object],
        router: [Getter],
        locals: [Object],
        _usedRouter: true },
     _headers: { 'x-powered-by': 'Express' },
     _headerNames: { 'x-powered-by': 'X-Powered-By' },
     req: [Circular],
     viewCallbacks: [],
     locals: [Function: locals] },
  next: [Function: next],
  body: { description: 'Uploaded from my phone' },
  files: 
   { file: 
      { domain: null,
        _events: {},
        _maxListeners: 10,
        size: 13113,
        path: '/tmp/2d360f1287f0a12e150d02fb30a07003',
        name: 'filename.jpg',
        type: 'image/jpeg',
        hash: false,
        lastModifiedDate: Thu Aug 14 2014 21:46:28 GMT+0530 (IST),
        _writeStream: [Object],
        length: [Getter],
        filename: [Getter],
        mime: [Getter] } },
  _body: true,
  pipe: [Function],
  addListener: [Function],
  on: [Function],
  pause: [Function],
  resume: [Function],
  read: [Function],
  _route_index: 0,
  route: 
   { path: '/upload',
     method: 'post',
     callbacks: [ [Function] ],
     keys: [],
     regexp: /^\/upload\/?$/i,
     params: [] },
  params: [] }
req
OrangeDog commented 9 years ago

Oh, if you're using express then that already contains formidable and will have already parsed the upload for you. You should have said that to start with.

macatadobe commented 9 years ago

so what should i do next ?

macatadobe commented 9 years ago

guys , please help ...

zyywolf commented 9 years ago

i catched the exception too,did u solved,?

yogeshbansal commented 5 years ago

Is this issue resolved?? please let me know solution if anyone has

xarguments commented 5 years ago

Do you use NginX or other stuff that could halt the stream? Do you receive files inside the "parse" event? If yes, is the "file.size" set in the file?

@zyywolf , @yogeshbansal could you please provide chunk of code you used? It may be somewhat different, and may help.

Thanks.

GrosSacASac commented 3 years ago

Very old issue, closing but we can reopen if we have more info

WheelerLee commented 1 year ago

Uploading works fine from the browser, but an error occurs when uploading from Postman.

GrosSacASac commented 1 year ago

@WheelerLee we need more info to reproduce the issue.

Version ? What are you sending with postman ? Server Code ?

WheelerLee commented 1 year ago

@GrosSacASac Sorry, I made a mistake.